Move notifier logic to core 88/14688/1
authorsh.pi <sh.pi@samsung.com>
Thu, 11 Apr 2013 02:17:14 +0000 (11:17 +0900)
committerKrzysztof Sasiak <k.sasiak@samsung.com>
Thu, 9 Jan 2014 16:36:13 +0000 (17:36 +0100)
It's need for internal interface among device modules.

Change-Id: I994d56ac2f33718253e98bf9f4cd05036ab66d22
Signed-off-by: Krzysztof Sasiak <k.sasiak@samsung.com>
CMakeLists.txt
display/core.c
display/core.h
src/core/device-notifier.c [new file with mode: 0644]
src/core/device-notifier.h [new file with mode: 0644]
src/core/devices.c
src/core/devices.h
src/core/list.h [new file with mode: 0644]

index 797b659..7eced1d 100755 (executable)
@@ -13,6 +13,7 @@ SET(VERSION 0.1.0)
 SET(SRCS
        src/battery/lowbat-handler.c
        src/bs/bs.c
+       src/core/device-notifier.c
        src/core/main.c
        src/core/sysnoti.c
        src/core/launch.c
index 6257492..9b3afeb 100644 (file)
@@ -46,6 +46,7 @@
 #include "core/queue.h"
 #include "core/data.h"
 #include "core/devices.h"
+#include "core/device-notifier.h"
 
 #define USB_CON_PIDFILE                        "/var/run/.system_server.pid"
 #define PM_STATE_LOG_FILE              "/var/log/pm_state.log"
@@ -263,99 +264,6 @@ static Eina_Bool del_sleep_cond(void *data)
        return EINA_FALSE;
 }
 
-struct pm_listener {
-       enum pm_status status;
-       int (*func)(void *data);
-};
-
-Eina_List *pm_listener_list;
-
-int pm_add_listener(enum pm_status status, int (*func)(void *data))
-{
-       Eina_List *n;
-       struct pm_listener *data, *listener;
-
-       LOGINFO("%d, %x", status, func);
-
-       if (!func) {
-               LOGERR("invalid func address!");
-               return -1;
-       }
-
-       EINA_LIST_FOREACH(pm_listener_list, n, data)
-               if (status == data->status && func == (data->func)) {
-                       LOGERR("function is already registered! [%d, %x]",
-                           status, func);
-                       return -1;
-               }
-
-       listener = malloc(sizeof(struct pm_listener));
-       if (!listener) {
-               LOGERR("Fail to malloc for listener!");
-               return -1;
-       }
-
-       listener->status = status;
-       listener->func = func;
-
-       pm_listener_list = eina_list_append(pm_listener_list, listener);
-
-       return 0;
-}
-
-int pm_del_listener(enum pm_status status, int (*func)(void *data))
-{
-       Eina_List *n, *next;
-       struct pm_listener *listener;
-
-       if (!func) {
-               LOGERR("invalid func address!");
-               return -1;
-       }
-
-       EINA_LIST_FOREACH_SAFE(pm_listener_list, n, next, listener)
-               if (status == listener->status && func == (listener->func)) {
-                       LOGINFO("[%d, %x]", status, func);
-                       free(listener);
-                       pm_listener_list = eina_list_remove_list(
-                                           pm_listener_list, n);
-               }
-
-       return 0;
-}
-
-static void pm_del_listener_all(void)
-{
-       Eina_List *n, *next;
-       struct pm_listener *listener;
-
-       EINA_LIST_FOREACH_SAFE(pm_listener_list, n, next, listener)
-               if (listener) {
-                       free(listener);
-                       pm_listener_list = eina_list_remove_list(pm_listener_list, n);
-               }
-
-       LOGINFO("all deleted!");
-}
-
-void pm_talker(enum pm_status status, void *data)
-{
-       Eina_List *n;
-       struct pm_listener *listener;
-       int cnt = 0;
-
-       EINA_LIST_FOREACH(pm_listener_list, n, listener) {
-               if (status == listener->status) {
-                       if (listener->func) {
-                               listener->func(data);
-                               cnt++;
-                       }
-               }
-       }
-
-       LOGINFO("cb is called! status:%d, cnt:%d ", status, cnt);
-}
-
 /* update transition condition for application requrements */
 static int proc_condition(PMMsg *data)
 {
@@ -871,7 +779,7 @@ static int default_action(int timeout)
 
        if (pm_cur_state != pm_old_state && pm_cur_state != S_SLEEP) {
                set_setting_pmstate(pm_cur_state);
-               pm_talker(PM_STATUS_LCD, pm_cur_state);
+               device_notify(DEVICE_NOTIFIER_LCD, pm_cur_state);
        }
        switch (pm_cur_state) {
        case S_NORMAL:
@@ -1624,7 +1532,6 @@ static void end_pm_main(void *data)
        int i;
 
        end_battinfo_gathering();
-       pm_del_listener_all();
 
        for (i = i - 1; i >= INIT_SETTING; i--) {
                switch (i) {
index 46cfd12..691dafb 100644 (file)
@@ -98,15 +98,6 @@ struct state {
        int timeout;
 } states[S_END];
 
-enum pm_status {
-       PM_STATUS_LCD,
-       PM_STATUS_MAX
-};
-
-int pm_add_listener(enum pm_status status, int (*func)(void *data));
-int pm_del_listener(enum pm_status status, int (*func)(void *data));
-void pm_talker(enum pm_status status, void *value);
-
 /* If the bit in a condition variable is set,
  *  we cannot transit the state until clear this bit. */
 int trans_condition;
diff --git a/src/core/device-notifier.c b/src/core/device-notifier.c
new file mode 100644 (file)
index 0000000..f78b99b
--- /dev/null
@@ -0,0 +1,124 @@
+/*
+ * deviced
+ *
+ * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * 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 <Ecore.h>
+
+#include "log.h"
+#include "devices.h"
+#include "device-notifier.h"
+#include "list.h"
+
+struct device_notifier {
+       enum device_notifier_type status;
+       int (*func)(void *data);
+};
+
+static Eina_List *device_notifier_list;
+
+#define FIND_NOTIFIER(a, b, c, d, e, f) \
+       EINA_LIST_FOREACH_SAFE(a, b, c, d) \
+               if (e == d->e && f == (d->f))
+
+int register_notifier(enum device_notifier_type status, int (*func)(void *data))
+{
+       Eina_List *n, *next;
+       struct device_notifier *data, *notifier;
+
+       PRT_TRACE("%d, %x", status, func);
+
+       if (!func) {
+               PRT_TRACE_ERR("invalid func address!");
+               return -EINVAL;
+       }
+
+       FIND_NOTIFIER(device_notifier_list, n, next, notifier, status, func) {
+               PRT_TRACE_ERR("function is already registered! [%d, %x]",
+                   status, func);
+               return -EINVAL;
+       }
+
+       notifier = malloc(sizeof(struct device_notifier));
+       if (!notifier) {
+               PRT_TRACE_ERR("Fail to malloc for notifier!");
+               return -ENOMEM;
+       }
+
+       notifier->status = status;
+       notifier->func = func;
+
+       EINA_LIST_APPEND(device_notifier_list, notifier);
+
+       return 0;
+}
+
+int unregister_notifier(enum device_notifier_type status, int (*func)(void *data))
+{
+       Eina_List *n, *next;
+       struct device_notifier *notifier;
+
+       if (!func) {
+               PRT_TRACE_ERR("invalid func address!");
+               return -EINVAL;
+       }
+
+       FIND_NOTIFIER(device_notifier_list, n, next, notifier, status, func) {
+               PRT_TRACE("[%d, %x]", status, func);
+               free(notifier);
+               EINA_LIST_REMOVE_LIST(device_notifier_list, n);
+       }
+
+       return 0;
+}
+
+void device_notify(enum device_notifier_type status, void *data)
+{
+       Eina_List *n;
+       struct device_notifier *notifier;
+       int cnt = 0;
+
+       EINA_LIST_FOREACH(device_notifier_list, n, notifier) {
+               if (status == notifier->status) {
+                       if (notifier->func) {
+                               notifier->func(data);
+                               cnt++;
+                       }
+               }
+       }
+
+       PRT_TRACE("cb is called! status:%d, cnt:%d ", status, cnt);
+}
+
+static void device_notifier_exit(void)
+{
+       Eina_List *n, *next;
+       struct device_notifier *notifier;
+
+       EINA_LIST_FOREACH_SAFE(device_notifier_list, n, next, notifier)
+               if (notifier) {
+                       free(notifier);
+                       EINA_LIST_REMOVE_LIST(device_notifier_list, n);
+               }
+
+       PRT_TRACE("all deleted!");
+}
+
+const struct device_ops notifier_device_ops = {
+       .exit = device_notifier_exit,
+};
+
diff --git a/src/core/device-notifier.h b/src/core/device-notifier.h
new file mode 100644 (file)
index 0000000..d633145
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * deviced
+ *
+ * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * 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 __DEVICE_NOTIFIER_H__
+#define __DEVICE_NOTIFIER_H__
+
+enum device_notifier_type {
+       DEVICE_NOTIFIER_LCD,
+       DEVICE_NOTIFIER_MAX,
+};
+
+/*
+ * This is for internal callback method.
+ */
+int register_notifier(enum device_notifier_type status, int (*func)(void *data));
+int unregister_notifier_del(enum device_notifier_type status, int (*func)(void *data));
+void device_notify(enum device_notifier_type status, void *value);
+
+#endif /* __DEVICE_NOTIFIER_H__ */
index 13a85b7..99340f5 100644 (file)
@@ -47,6 +47,7 @@ static const struct device_ops *devices[] = {
        &mmc_device_ops,
        &led_device_ops,
        &vibrator_device_ops,
+       &notifier_device_ops,
 };
 
 void devices_init(void *data)
index 308df65..05cec5a 100644 (file)
@@ -69,5 +69,6 @@ extern const struct device_ops pmon_device_ops;
 extern const struct device_ops mmc_device_ops;
 extern const struct device_ops led_device_ops;
 extern const struct device_ops vibrator_device_ops;
+extern const struct device_ops notifier_device_ops;
 
 #endif
diff --git a/src/core/list.h b/src/core/list.h
new file mode 100644 (file)
index 0000000..6251e0a
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * deviced
+ *
+ * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * 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 __LIST_H__
+#define __LIST_H__
+
+#include <Ecore.h>
+
+#define EINA_LIST_APPEND(a, b) \
+       a = eina_list_append(a, b)
+
+#define EINA_LIST_REMOVE(a, b) \
+       a = eina_list_remove(a, b)
+
+#define EINA_LIST_REMOVE_LIST(a, b) \
+       a = eina_list_remove_list(a, b)
+
+#define EINA_LIST_FREE(a) \
+       a = eina_list_free(a)
+
+#endif