pass: Change directory name from core to util 73/268373/1
authorChanwoo Choi <cw00.choi@samsung.com>
Wed, 22 Dec 2021 03:00:38 +0000 (12:00 +0900)
committerChanwoo Choi <cw00.choi@samsung.com>
Wed, 22 Dec 2021 03:00:38 +0000 (12:00 +0900)
Change directory name from core to util because 'src/core' directory
contains the util functions and then move 'src/util/main.c' to src/main.c
in order to improve the readability of PASS daemon starting point.

Change-Id: I26d421d67ff4790c82e2a1af22d208104ad136db
Signed-off-by: Chanwoo Choi <cw00.choi@samsung.com>
12 files changed:
CMakeLists.txt
src/core/common.c [deleted file]
src/core/device-notifier.c [deleted file]
src/core/devices.c [deleted file]
src/core/gdbus-util.c [deleted file]
src/core/main.c [deleted file]
src/main.c [new file with mode: 0644]
src/util/common.c [new file with mode: 0644]
src/util/device-notifier.c [new file with mode: 0644]
src/util/devices.c [new file with mode: 0644]
src/util/gdbus-util.c [new file with mode: 0644]
unittest/CMakeLists.txt

index b38116605a651850e122233144d4502362aca1ea..c9af4c3a639c0c096d1b1a2c394f1b9e7aaa61eb 100644 (file)
@@ -24,11 +24,11 @@ SET(SRCS
        src/pmqos/pmqos-parser.c
        src/thermal/thermal.c
        src/thermal/thermal-parser.c
-       src/core/common.c
-       src/core/device-notifier.c
-       src/core/devices.c
-       src/core/main.c
-       src/core/gdbus-util.c
+       src/util/common.c
+       src/util/device-notifier.c
+       src/util/devices.c
+       src/util/gdbus-util.c
+       src/main.c
        #Generated by a custom command 'gdbus-codegen' below
        src/pass/pass-dbus-stub.c
        #Generated by a custom command 'gdbus-codegen' below
diff --git a/src/core/common.c b/src/core/common.c
deleted file mode 100644 (file)
index 0497e28..0000000
+++ /dev/null
@@ -1,124 +0,0 @@
-/*
- * PASS
- *
- * 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 <stdio.h>
-#include <stdlib.h>
-#include <stdbool.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <time.h>
-#include <fcntl.h>
-#include <signal.h>
-#include <dirent.h>
-#include <ctype.h>
-#include <string.h>
-#include <stdarg.h>
-#include <errno.h>
-#include <poll.h>
-#include <mntent.h>
-#include <limits.h>
-
-#include <pass/log.h>
-#include <pass/common.h>
-
-#define BUFF_MAX       255
-
-static int sys_read_buf(const char *file, char *buf)
-{
-       int fd;
-       int r;
-       int ret = 0;
-
-       fd = open(file, O_RDONLY);
-       if (fd == -1)
-               return -ENOENT;
-
-       r = read(fd, buf, BUFF_MAX);
-       if ((r >= 0) && (r < BUFF_MAX))
-               buf[r] = '\0';
-       else
-               ret = -EIO;
-
-       close(fd);
-
-       return ret;
-}
-
-int sys_get_str(const char *fname, char *str)
-{
-       char buf[BUFF_MAX] = {0};
-
-       if (sys_read_buf(fname, buf) == 0) {
-               snprintf(str, strlen(buf) + 1, "%s", buf);
-               return 0;
-       }
-
-       return -1;
-}
-
-const char *get_string_from_object(json_object *obj, const char *key)
-{
-       json_object *tmp = NULL;
-
-       if (!json_object_object_get_ex(obj, key, &tmp))
-               return NULL;
-
-       return json_object_get_string(tmp);
-}
-
-const int get_int_from_object(json_object *obj, const char *key)
-{
-       json_object *tmp = NULL;
-
-       if (!json_object_object_get_ex(obj, key, &tmp))
-               return -EINVAL;
-
-       return json_object_get_int(tmp);
-}
-
-const double get_double_from_object(json_object *obj, const char *key)
-{
-       json_object *tmp = NULL;
-
-       if (!json_object_object_get_ex(obj, key, &tmp))
-               return -EINVAL;
-
-       return json_object_get_double(tmp);
-}
-
-const int get_boolean_from_object(json_object *obj, const char *key)
-{
-       json_object *tmp = NULL;
-
-       if (!json_object_object_get_ex(obj, key, &tmp))
-               return -EINVAL;
-
-       return (int)json_object_get_boolean(tmp);
-}
-
-json_object *get_object_from_object(json_object *obj, const char *key)
-{
-       json_object *tmp = NULL;
-
-       if (!json_object_object_get_ex(obj, key, &tmp))
-               return NULL;
-
-       return tmp;
-}
diff --git a/src/core/device-notifier.c b/src/core/device-notifier.c
deleted file mode 100644 (file)
index 2c6e99b..0000000
+++ /dev/null
@@ -1,131 +0,0 @@
-/*
- * PASS
- *
- * 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 <glib.h>
-
-#include <pass/common.h>
-#include <pass/device-notifier.h>
-#include <pass/log.h>
-
-#define DEVICE_NOTIFIER_MAX_COUNT      255
-
-static GList *device_notifier_list;
-static guint idle_func_id = 0;
-static struct device_notifier device_notifiers[DEVICE_NOTIFIER_MAX_COUNT];
-
-static struct device_notifier *get_device_notifier(void)
-{
-       static int pos = 0;
-
-       if ((pos < DEVICE_NOTIFIER_MAX_COUNT) &&
-               (!device_notifiers[pos].is_used)) {
-               device_notifiers[pos].is_used = true;
-               return &device_notifiers[pos++];
-       } else {
-               for (pos = 0; pos < DEVICE_NOTIFIER_MAX_COUNT; pos++) {
-                       if (!device_notifiers[pos].is_used) {
-                               device_notifiers[pos].is_used = true;
-                               return &device_notifiers[pos];
-                       }
-               }
-       }
-
-       return NULL;
-}
-
-int register_notifier(enum device_notifier_type status,
-               int (*func)(void *data, void *user_data), void *user_data)
-{
-       struct device_notifier *notifier;
-
-       if (status >= DEVICE_NOTIFIER_MAX)
-               return -EINVAL;
-
-       if (!func)
-               return -EINVAL;
-
-       notifier = get_device_notifier();
-       if (!notifier)
-               return -ENOMEM;
-
-       notifier->status = status;
-       notifier->func = func;
-       notifier->user_data = user_data;
-
-       device_notifier_list = g_list_append(device_notifier_list,
-                       (gpointer)notifier);
-
-       return 0;
-}
-
-int unregister_notifier(enum device_notifier_type status,
-               int (*func)(void *data, void *user_data), void *user_data)
-{
-       GList *n;
-       struct device_notifier *notifier;
-
-       for (n = device_notifier_list; n != NULL; n = n->next) {
-               notifier = n->data;
-               if ((notifier->status == status) &&
-                       (notifier->func == func) &&
-                       (notifier->user_data == user_data))
-                       notifier->is_used = false;
-       }
-
-       return 0;
-}
-
-static gboolean delete_unused_notifier_cb(gpointer user_data)
-{
-       GList *list;
-       GList *next;
-       struct device_notifier *notifier;
-
-       list = device_notifier_list;
-       while (list != NULL) {
-               notifier = (struct device_notifier *)list->data;
-               next = list->next;
-               if (!notifier->is_used) {
-                       memset(notifier, 0, sizeof(*notifier));
-                       device_notifier_list = g_list_remove_link(
-                                       device_notifier_list,
-                                       list);
-               }
-               list = next;
-       }
-       idle_func_id = 0;
-
-       return false;
-}
-
-void device_notify(enum device_notifier_type status, void *data)
-{
-       GList *n;
-       struct device_notifier *notifier;
-
-       for (n = device_notifier_list; n != NULL; n = n->next) {
-               notifier = n->data;
-               if (notifier->is_used && status == notifier->status) {
-                       if (notifier->func)
-                               notifier->func(data, notifier->user_data);
-               }
-       }
-
-       if (!idle_func_id)
-               idle_func_id = g_idle_add(delete_unused_notifier_cb, NULL);
-}
diff --git a/src/core/devices.c b/src/core/devices.c
deleted file mode 100644 (file)
index 296c047..0000000
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * PASS (Power Aware System Service)
- *
- * 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 <glib.h>
-#include <stdio.h>
-
-#include <pass/common.h>
-#include <pass/devices.h>
-#include <pass/log.h>
-
-static GList *dev_head;
-
-void add_device(const struct device_ops *dev)
-{
-       if (dev->priority == DEVICE_PRIORITY_HIGH)
-               dev_head = g_list_prepend(dev_head, (gpointer)dev);
-       else
-               dev_head = g_list_append(dev_head, (gpointer)dev);
-}
-
-void remove_device(const struct device_ops *dev)
-{
-       dev_head = g_list_remove(dev_head, (gconstpointer)dev);
-}
-
-void devices_init(void *data)
-{
-       GList *elem, *elem_n;
-       const struct device_ops *dev;
-
-       elem = dev_head;
-       while (elem != NULL) {
-               dev = elem->data;
-               elem_n = elem->next;
-               if (dev->probe && dev->probe(data) != 0) {
-                       _E("[%s] probe fail", dev->name);
-                       dev_head = g_list_remove(dev_head, (gconstpointer)dev);
-                       elem = elem_n;
-                       continue;
-               }
-               if (dev->init)
-                       dev->init(data);
-               elem = elem_n;
-       }
-}
-
-void devices_exit(void *data)
-{
-       GList *elem;
-       const struct device_ops *dev;
-
-       for (elem = dev_head; elem != NULL; elem = elem->next) {
-               dev = elem->data;
-               if (dev->exit)
-                       dev->exit(data);
-       }
-}
diff --git a/src/core/gdbus-util.c b/src/core/gdbus-util.c
deleted file mode 100644 (file)
index eca425d..0000000
+++ /dev/null
@@ -1,237 +0,0 @@
-/*
- * PASS (Power Aware System Service)
- *
- * 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 <errno.h>
-#include <stdbool.h>
-#include <systemd/sd-daemon.h>
-
-#include <pass/gdbus-util.h>
-#include <pass/log.h>
-
-static GDBusConnection *g_dbus_sys_conn[PASS_DBUS_MAX] = {NULL, };
-static int dbus_name_owned;
-
-int pass_gdbus_export_interface(passdbus idx, gpointer instance,
-                               const char *obj_path)
-{
-       gboolean ret;
-
-       if (idx >= PASS_DBUS_MAX) {
-               _E("Invalid DBUS connection: %d\n", idx);
-               return -EINVAL;
-       }
-
-       if (g_dbus_sys_conn[idx] == NULL) {
-               _E("cannot get the dbus connection "
-                               "to the system message bus\n");
-               return -ENOSYS;
-       }
-
-       ret = g_dbus_interface_skeleton_export(
-                       G_DBUS_INTERFACE_SKELETON(instance),
-                       g_dbus_sys_conn[idx],
-                       obj_path,
-                       NULL);
-       if (ret == TRUE)
-               return 0;
-
-       return -1;
-}
-
-static void pass_name_acquired_cb(GDBusConnection *connection,
-                               const gchar *name, gpointer user_data)
-{
-       if (++dbus_name_owned == PASS_DBUS_MAX)
-               sd_notify(0, "READY=1");
-}
-
-int pass_gdbus_get_name(passdbus idx, const char *name)
-{
-       guint id;
-
-       if (idx >= PASS_DBUS_MAX) {
-               _E("Invalid DBUS connection: %d\n", idx);
-               return -EINVAL;
-       }
-
-       id = g_bus_own_name_on_connection(g_dbus_sys_conn[idx], name,
-                       G_BUS_NAME_OWNER_FLAGS_NONE,
-                       pass_name_acquired_cb, NULL, NULL, NULL);
-       if (id == 0)
-               return -ENOSYS;
-
-       return 0;
-}
-
-int pass_gdbus_connect_signal(gpointer instance, int num_signals,
-               struct pass_gdbus_signal_info *signal_infos)
-{
-       int i;
-       unsigned long handler_id;
-
-       for (i = 0; i < num_signals; i++) {
-               handler_id = g_signal_connect(instance,
-                               signal_infos[i].handler,
-                               signal_infos[i].cb,
-                               signal_infos[i].cb_data);
-               if (handler_id <= 0)
-                       goto out_err;
-               signal_infos[i].ret_id = handler_id;
-       }
-
-       return 0;
-
-out_err:
-       for (i = 0; i < num_signals; i++) {
-               if (signal_infos[i].ret_id > 0) {
-                       g_signal_handler_disconnect(instance,
-                                       signal_infos[i].ret_id);
-                       signal_infos[i].ret_id = 0;
-               }
-       }
-
-       return -EINVAL;
-}
-
-void pass_gdbus_disconnect_signal(gpointer instance, int num_signals,
-               struct pass_gdbus_signal_info *signal_infos)
-{
-       int i;
-
-       for (i = 0; i < num_signals; i++) {
-               if (signal_infos[i].ret_id > 0) {
-                       g_signal_handler_disconnect(instance,
-                                       signal_infos[i].ret_id);
-                       signal_infos[i].ret_id = 0;
-               }
-       }
-}
-
-int pass_gdbus_send_broadcast_signal(passdbus idx, char *path,
-               char *interface, char *method, GVariant *arg)
-{
-       GDBusMessage *message = NULL;
-       GError *err = NULL;
-       int ret;
-
-       if (!path || !interface || !method || !arg) {
-               _E("invalid parameter\n");
-               return -EINVAL;
-       }
-
-       if (idx >= PASS_DBUS_MAX) {
-               _E("Invalid DBUS connection: %d\n", idx);
-               return -EINVAL;
-       }
-
-       if (!g_dbus_sys_conn[idx]) {
-               _E("cannot get the dbus connection to system message bus\n");
-               return -ENOSYS;
-       }
-
-       /* Make the dbus message to send broadcast signal */
-       message = g_dbus_message_new_signal(path, interface, method);
-       if (!message) {
-               _E("failed to allocate new %s.%s signal", interface, method);
-               return -EPERM;
-       }
-       g_dbus_message_set_body(message, arg);
-
-       /* Send broadcast signal */
-       ret = g_dbus_connection_send_message(g_dbus_sys_conn[idx], message,
-                               G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &err);
-       if (!ret) {
-               _E("failed to broadcast [%s]", method);
-               g_object_unref(message);
-               return -ECOMM;
-       }
-
-       g_object_unref(message);
-
-       return 0;
-}
-
-static void put_instance(gpointer *instance)
-{
-       g_object_unref(*instance);
-       *instance = NULL;
-}
-
-SystemPassCore *pass_gdbus_get_instance_core(void)
-{
-       return system_pass_core_skeleton_new();
-}
-
-void pass_gdbus_put_instance_core(SystemPassCore **instance)
-{
-       put_instance((gpointer *)instance);
-}
-
-SystemPassPmqos *pass_gdbus_get_instance_pmqos(void)
-{
-       return system_pass_pmqos_skeleton_new();
-}
-
-void pass_gdbus_put_instance_pmqos(SystemPassPmqos **instance)
-{
-       put_instance((gpointer *)instance);
-}
-
-SystemThermal *pass_gdbus_get_instance_thermal(void)
-{
-       return system_thermal_skeleton_new();
-}
-
-void pass_gdbus_put_instance_thermal(SystemThermal **instance)
-{
-       put_instance((gpointer *)instance);
-}
-
-int pass_gdbus_get_system_connection(passdbus idx)
-{
-       GError *error = NULL;
-
-       if (idx >= PASS_DBUS_MAX) {
-               _E("Invalid connection: %d\n", idx);
-               return -EINVAL;
-       }
-
-       g_dbus_sys_conn[idx] = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL,
-                       &error);
-       if (g_dbus_sys_conn[idx] == NULL) {
-               _E("cannot connect to the system message bus: %s\n",
-                               error->message);
-               return -ENOSYS;
-       }
-
-       return 0;
-}
-
-void pass_gdbus_put_system_connection(passdbus idx)
-{
-       if (idx >= PASS_DBUS_MAX) {
-               _E("Invalid DBUS connection: %d\n", idx);
-               return;
-       }
-
-       if (g_dbus_sys_conn[idx]) {
-               g_object_unref(g_dbus_sys_conn[idx]);
-               g_dbus_sys_conn[idx] = NULL;
-               dbus_name_owned--;
-       }
-}
diff --git a/src/core/main.c b/src/core/main.c
deleted file mode 100644 (file)
index d93a23c..0000000
+++ /dev/null
@@ -1,78 +0,0 @@
-/*
- * PASS (Power Aware System Service)
- *
- * 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 <stdio.h>
-#include <fcntl.h>
-#include <glib.h>
-#include <string.h>
-#include <gio/gio.h>
-#include <sys/reboot.h>
-
-#include <pass/common.h>
-#include <pass/device-notifier.h>
-#include <pass/devices.h>
-#include <pass/gdbus-util.h>
-#include <pass/log.h>
-
-GMainLoop *g_mainloop;
-
-static void sig_quit(int signo)
-{
-       _D("received SIGTERM signal %d", signo);
-}
-
-static void sig_usr1(int signo)
-{
-       _D("received SIGUSR1 signal %d", signo);
-
-       g_main_loop_quit(g_mainloop);
-}
-
-static int late_init(void)
-{
-       int ret;
-
-       signal(SIGTERM, sig_quit);
-       signal(SIGUSR1, sig_usr1);
-
-       ret = pass_gdbus_get_name(PASS_DBUS_CORE, DBUS_PASS_BUS_NAME);
-       if (ret < 0)
-               return ret;
-
-       device_notify(DEVICE_NOTIFIER_INIT_DONE, NULL);
-
-       return 0;
-}
-
-int main(int argc, char **argv)
-{
-       g_mainloop = g_main_loop_new(NULL, FALSE);
-       pass_gdbus_get_system_connection(PASS_DBUS_CORE);
-
-       devices_init(NULL);
-       if (late_init() < 0)
-               _E("cannot init pass core system\n");
-
-       g_main_loop_run(g_mainloop);
-
-       devices_exit(NULL);
-       pass_gdbus_put_system_connection(PASS_DBUS_CORE);
-       g_main_loop_unref(g_mainloop);
-
-       return 0;
-}
diff --git a/src/main.c b/src/main.c
new file mode 100644 (file)
index 0000000..d93a23c
--- /dev/null
@@ -0,0 +1,78 @@
+/*
+ * PASS (Power Aware System Service)
+ *
+ * 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 <stdio.h>
+#include <fcntl.h>
+#include <glib.h>
+#include <string.h>
+#include <gio/gio.h>
+#include <sys/reboot.h>
+
+#include <pass/common.h>
+#include <pass/device-notifier.h>
+#include <pass/devices.h>
+#include <pass/gdbus-util.h>
+#include <pass/log.h>
+
+GMainLoop *g_mainloop;
+
+static void sig_quit(int signo)
+{
+       _D("received SIGTERM signal %d", signo);
+}
+
+static void sig_usr1(int signo)
+{
+       _D("received SIGUSR1 signal %d", signo);
+
+       g_main_loop_quit(g_mainloop);
+}
+
+static int late_init(void)
+{
+       int ret;
+
+       signal(SIGTERM, sig_quit);
+       signal(SIGUSR1, sig_usr1);
+
+       ret = pass_gdbus_get_name(PASS_DBUS_CORE, DBUS_PASS_BUS_NAME);
+       if (ret < 0)
+               return ret;
+
+       device_notify(DEVICE_NOTIFIER_INIT_DONE, NULL);
+
+       return 0;
+}
+
+int main(int argc, char **argv)
+{
+       g_mainloop = g_main_loop_new(NULL, FALSE);
+       pass_gdbus_get_system_connection(PASS_DBUS_CORE);
+
+       devices_init(NULL);
+       if (late_init() < 0)
+               _E("cannot init pass core system\n");
+
+       g_main_loop_run(g_mainloop);
+
+       devices_exit(NULL);
+       pass_gdbus_put_system_connection(PASS_DBUS_CORE);
+       g_main_loop_unref(g_mainloop);
+
+       return 0;
+}
diff --git a/src/util/common.c b/src/util/common.c
new file mode 100644 (file)
index 0000000..0497e28
--- /dev/null
@@ -0,0 +1,124 @@
+/*
+ * PASS
+ *
+ * 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 <stdio.h>
+#include <stdlib.h>
+#include <stdbool.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <time.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <dirent.h>
+#include <ctype.h>
+#include <string.h>
+#include <stdarg.h>
+#include <errno.h>
+#include <poll.h>
+#include <mntent.h>
+#include <limits.h>
+
+#include <pass/log.h>
+#include <pass/common.h>
+
+#define BUFF_MAX       255
+
+static int sys_read_buf(const char *file, char *buf)
+{
+       int fd;
+       int r;
+       int ret = 0;
+
+       fd = open(file, O_RDONLY);
+       if (fd == -1)
+               return -ENOENT;
+
+       r = read(fd, buf, BUFF_MAX);
+       if ((r >= 0) && (r < BUFF_MAX))
+               buf[r] = '\0';
+       else
+               ret = -EIO;
+
+       close(fd);
+
+       return ret;
+}
+
+int sys_get_str(const char *fname, char *str)
+{
+       char buf[BUFF_MAX] = {0};
+
+       if (sys_read_buf(fname, buf) == 0) {
+               snprintf(str, strlen(buf) + 1, "%s", buf);
+               return 0;
+       }
+
+       return -1;
+}
+
+const char *get_string_from_object(json_object *obj, const char *key)
+{
+       json_object *tmp = NULL;
+
+       if (!json_object_object_get_ex(obj, key, &tmp))
+               return NULL;
+
+       return json_object_get_string(tmp);
+}
+
+const int get_int_from_object(json_object *obj, const char *key)
+{
+       json_object *tmp = NULL;
+
+       if (!json_object_object_get_ex(obj, key, &tmp))
+               return -EINVAL;
+
+       return json_object_get_int(tmp);
+}
+
+const double get_double_from_object(json_object *obj, const char *key)
+{
+       json_object *tmp = NULL;
+
+       if (!json_object_object_get_ex(obj, key, &tmp))
+               return -EINVAL;
+
+       return json_object_get_double(tmp);
+}
+
+const int get_boolean_from_object(json_object *obj, const char *key)
+{
+       json_object *tmp = NULL;
+
+       if (!json_object_object_get_ex(obj, key, &tmp))
+               return -EINVAL;
+
+       return (int)json_object_get_boolean(tmp);
+}
+
+json_object *get_object_from_object(json_object *obj, const char *key)
+{
+       json_object *tmp = NULL;
+
+       if (!json_object_object_get_ex(obj, key, &tmp))
+               return NULL;
+
+       return tmp;
+}
diff --git a/src/util/device-notifier.c b/src/util/device-notifier.c
new file mode 100644 (file)
index 0000000..2c6e99b
--- /dev/null
@@ -0,0 +1,131 @@
+/*
+ * PASS
+ *
+ * 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 <glib.h>
+
+#include <pass/common.h>
+#include <pass/device-notifier.h>
+#include <pass/log.h>
+
+#define DEVICE_NOTIFIER_MAX_COUNT      255
+
+static GList *device_notifier_list;
+static guint idle_func_id = 0;
+static struct device_notifier device_notifiers[DEVICE_NOTIFIER_MAX_COUNT];
+
+static struct device_notifier *get_device_notifier(void)
+{
+       static int pos = 0;
+
+       if ((pos < DEVICE_NOTIFIER_MAX_COUNT) &&
+               (!device_notifiers[pos].is_used)) {
+               device_notifiers[pos].is_used = true;
+               return &device_notifiers[pos++];
+       } else {
+               for (pos = 0; pos < DEVICE_NOTIFIER_MAX_COUNT; pos++) {
+                       if (!device_notifiers[pos].is_used) {
+                               device_notifiers[pos].is_used = true;
+                               return &device_notifiers[pos];
+                       }
+               }
+       }
+
+       return NULL;
+}
+
+int register_notifier(enum device_notifier_type status,
+               int (*func)(void *data, void *user_data), void *user_data)
+{
+       struct device_notifier *notifier;
+
+       if (status >= DEVICE_NOTIFIER_MAX)
+               return -EINVAL;
+
+       if (!func)
+               return -EINVAL;
+
+       notifier = get_device_notifier();
+       if (!notifier)
+               return -ENOMEM;
+
+       notifier->status = status;
+       notifier->func = func;
+       notifier->user_data = user_data;
+
+       device_notifier_list = g_list_append(device_notifier_list,
+                       (gpointer)notifier);
+
+       return 0;
+}
+
+int unregister_notifier(enum device_notifier_type status,
+               int (*func)(void *data, void *user_data), void *user_data)
+{
+       GList *n;
+       struct device_notifier *notifier;
+
+       for (n = device_notifier_list; n != NULL; n = n->next) {
+               notifier = n->data;
+               if ((notifier->status == status) &&
+                       (notifier->func == func) &&
+                       (notifier->user_data == user_data))
+                       notifier->is_used = false;
+       }
+
+       return 0;
+}
+
+static gboolean delete_unused_notifier_cb(gpointer user_data)
+{
+       GList *list;
+       GList *next;
+       struct device_notifier *notifier;
+
+       list = device_notifier_list;
+       while (list != NULL) {
+               notifier = (struct device_notifier *)list->data;
+               next = list->next;
+               if (!notifier->is_used) {
+                       memset(notifier, 0, sizeof(*notifier));
+                       device_notifier_list = g_list_remove_link(
+                                       device_notifier_list,
+                                       list);
+               }
+               list = next;
+       }
+       idle_func_id = 0;
+
+       return false;
+}
+
+void device_notify(enum device_notifier_type status, void *data)
+{
+       GList *n;
+       struct device_notifier *notifier;
+
+       for (n = device_notifier_list; n != NULL; n = n->next) {
+               notifier = n->data;
+               if (notifier->is_used && status == notifier->status) {
+                       if (notifier->func)
+                               notifier->func(data, notifier->user_data);
+               }
+       }
+
+       if (!idle_func_id)
+               idle_func_id = g_idle_add(delete_unused_notifier_cb, NULL);
+}
diff --git a/src/util/devices.c b/src/util/devices.c
new file mode 100644 (file)
index 0000000..296c047
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * PASS (Power Aware System Service)
+ *
+ * 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 <glib.h>
+#include <stdio.h>
+
+#include <pass/common.h>
+#include <pass/devices.h>
+#include <pass/log.h>
+
+static GList *dev_head;
+
+void add_device(const struct device_ops *dev)
+{
+       if (dev->priority == DEVICE_PRIORITY_HIGH)
+               dev_head = g_list_prepend(dev_head, (gpointer)dev);
+       else
+               dev_head = g_list_append(dev_head, (gpointer)dev);
+}
+
+void remove_device(const struct device_ops *dev)
+{
+       dev_head = g_list_remove(dev_head, (gconstpointer)dev);
+}
+
+void devices_init(void *data)
+{
+       GList *elem, *elem_n;
+       const struct device_ops *dev;
+
+       elem = dev_head;
+       while (elem != NULL) {
+               dev = elem->data;
+               elem_n = elem->next;
+               if (dev->probe && dev->probe(data) != 0) {
+                       _E("[%s] probe fail", dev->name);
+                       dev_head = g_list_remove(dev_head, (gconstpointer)dev);
+                       elem = elem_n;
+                       continue;
+               }
+               if (dev->init)
+                       dev->init(data);
+               elem = elem_n;
+       }
+}
+
+void devices_exit(void *data)
+{
+       GList *elem;
+       const struct device_ops *dev;
+
+       for (elem = dev_head; elem != NULL; elem = elem->next) {
+               dev = elem->data;
+               if (dev->exit)
+                       dev->exit(data);
+       }
+}
diff --git a/src/util/gdbus-util.c b/src/util/gdbus-util.c
new file mode 100644 (file)
index 0000000..eca425d
--- /dev/null
@@ -0,0 +1,237 @@
+/*
+ * PASS (Power Aware System Service)
+ *
+ * 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 <errno.h>
+#include <stdbool.h>
+#include <systemd/sd-daemon.h>
+
+#include <pass/gdbus-util.h>
+#include <pass/log.h>
+
+static GDBusConnection *g_dbus_sys_conn[PASS_DBUS_MAX] = {NULL, };
+static int dbus_name_owned;
+
+int pass_gdbus_export_interface(passdbus idx, gpointer instance,
+                               const char *obj_path)
+{
+       gboolean ret;
+
+       if (idx >= PASS_DBUS_MAX) {
+               _E("Invalid DBUS connection: %d\n", idx);
+               return -EINVAL;
+       }
+
+       if (g_dbus_sys_conn[idx] == NULL) {
+               _E("cannot get the dbus connection "
+                               "to the system message bus\n");
+               return -ENOSYS;
+       }
+
+       ret = g_dbus_interface_skeleton_export(
+                       G_DBUS_INTERFACE_SKELETON(instance),
+                       g_dbus_sys_conn[idx],
+                       obj_path,
+                       NULL);
+       if (ret == TRUE)
+               return 0;
+
+       return -1;
+}
+
+static void pass_name_acquired_cb(GDBusConnection *connection,
+                               const gchar *name, gpointer user_data)
+{
+       if (++dbus_name_owned == PASS_DBUS_MAX)
+               sd_notify(0, "READY=1");
+}
+
+int pass_gdbus_get_name(passdbus idx, const char *name)
+{
+       guint id;
+
+       if (idx >= PASS_DBUS_MAX) {
+               _E("Invalid DBUS connection: %d\n", idx);
+               return -EINVAL;
+       }
+
+       id = g_bus_own_name_on_connection(g_dbus_sys_conn[idx], name,
+                       G_BUS_NAME_OWNER_FLAGS_NONE,
+                       pass_name_acquired_cb, NULL, NULL, NULL);
+       if (id == 0)
+               return -ENOSYS;
+
+       return 0;
+}
+
+int pass_gdbus_connect_signal(gpointer instance, int num_signals,
+               struct pass_gdbus_signal_info *signal_infos)
+{
+       int i;
+       unsigned long handler_id;
+
+       for (i = 0; i < num_signals; i++) {
+               handler_id = g_signal_connect(instance,
+                               signal_infos[i].handler,
+                               signal_infos[i].cb,
+                               signal_infos[i].cb_data);
+               if (handler_id <= 0)
+                       goto out_err;
+               signal_infos[i].ret_id = handler_id;
+       }
+
+       return 0;
+
+out_err:
+       for (i = 0; i < num_signals; i++) {
+               if (signal_infos[i].ret_id > 0) {
+                       g_signal_handler_disconnect(instance,
+                                       signal_infos[i].ret_id);
+                       signal_infos[i].ret_id = 0;
+               }
+       }
+
+       return -EINVAL;
+}
+
+void pass_gdbus_disconnect_signal(gpointer instance, int num_signals,
+               struct pass_gdbus_signal_info *signal_infos)
+{
+       int i;
+
+       for (i = 0; i < num_signals; i++) {
+               if (signal_infos[i].ret_id > 0) {
+                       g_signal_handler_disconnect(instance,
+                                       signal_infos[i].ret_id);
+                       signal_infos[i].ret_id = 0;
+               }
+       }
+}
+
+int pass_gdbus_send_broadcast_signal(passdbus idx, char *path,
+               char *interface, char *method, GVariant *arg)
+{
+       GDBusMessage *message = NULL;
+       GError *err = NULL;
+       int ret;
+
+       if (!path || !interface || !method || !arg) {
+               _E("invalid parameter\n");
+               return -EINVAL;
+       }
+
+       if (idx >= PASS_DBUS_MAX) {
+               _E("Invalid DBUS connection: %d\n", idx);
+               return -EINVAL;
+       }
+
+       if (!g_dbus_sys_conn[idx]) {
+               _E("cannot get the dbus connection to system message bus\n");
+               return -ENOSYS;
+       }
+
+       /* Make the dbus message to send broadcast signal */
+       message = g_dbus_message_new_signal(path, interface, method);
+       if (!message) {
+               _E("failed to allocate new %s.%s signal", interface, method);
+               return -EPERM;
+       }
+       g_dbus_message_set_body(message, arg);
+
+       /* Send broadcast signal */
+       ret = g_dbus_connection_send_message(g_dbus_sys_conn[idx], message,
+                               G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, &err);
+       if (!ret) {
+               _E("failed to broadcast [%s]", method);
+               g_object_unref(message);
+               return -ECOMM;
+       }
+
+       g_object_unref(message);
+
+       return 0;
+}
+
+static void put_instance(gpointer *instance)
+{
+       g_object_unref(*instance);
+       *instance = NULL;
+}
+
+SystemPassCore *pass_gdbus_get_instance_core(void)
+{
+       return system_pass_core_skeleton_new();
+}
+
+void pass_gdbus_put_instance_core(SystemPassCore **instance)
+{
+       put_instance((gpointer *)instance);
+}
+
+SystemPassPmqos *pass_gdbus_get_instance_pmqos(void)
+{
+       return system_pass_pmqos_skeleton_new();
+}
+
+void pass_gdbus_put_instance_pmqos(SystemPassPmqos **instance)
+{
+       put_instance((gpointer *)instance);
+}
+
+SystemThermal *pass_gdbus_get_instance_thermal(void)
+{
+       return system_thermal_skeleton_new();
+}
+
+void pass_gdbus_put_instance_thermal(SystemThermal **instance)
+{
+       put_instance((gpointer *)instance);
+}
+
+int pass_gdbus_get_system_connection(passdbus idx)
+{
+       GError *error = NULL;
+
+       if (idx >= PASS_DBUS_MAX) {
+               _E("Invalid connection: %d\n", idx);
+               return -EINVAL;
+       }
+
+       g_dbus_sys_conn[idx] = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL,
+                       &error);
+       if (g_dbus_sys_conn[idx] == NULL) {
+               _E("cannot connect to the system message bus: %s\n",
+                               error->message);
+               return -ENOSYS;
+       }
+
+       return 0;
+}
+
+void pass_gdbus_put_system_connection(passdbus idx)
+{
+       if (idx >= PASS_DBUS_MAX) {
+               _E("Invalid DBUS connection: %d\n", idx);
+               return;
+       }
+
+       if (g_dbus_sys_conn[idx]) {
+               g_object_unref(g_dbus_sys_conn[idx]);
+               g_dbus_sys_conn[idx] = NULL;
+               dbus_name_owned--;
+       }
+}
index b940c73946ff1eb2c0ac2b0bff2ef5785ab6a2ea..802fa5828dcfb698b53564fae1d11b41e97b44b6 100644 (file)
@@ -2,7 +2,7 @@ PROJECT(pass C CXX)
 
 SET(SRCS ${CMAKE_SOURCE_DIR}/src/pass/pass-hal.c
        ${CMAKE_SOURCE_DIR}/src/pass/pass-parser.c
-       ${CMAKE_SOURCE_DIR}/src/core/common.c
+       ${CMAKE_SOURCE_DIR}/src/util/common.c
 )
 
 INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR})