resource: Operate based on resource id
[platform/core/system/pass.git] / src / monitor / monitor.c
1 /*
2  * PASS (Power Aware System Service)
3  *
4  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 #include <glib.h>
20
21 #include <util/device-notifier.h>
22 #include <util/log.h>
23 #include <util/devices.h>
24 #include <util/gdbus-util.h>
25 #include <monitor/monitor.h>
26
27 #include <libsyscommon/resource-manager.h>
28
29 static struct monitor g_monitor;
30 static gboolean g_debug_mode;
31
32 static SystemPassMonitor *g_gdbus_instance;
33
34 struct monitor *monitor_get(void)
35 {
36         return &g_monitor;
37 }
38
39 gboolean monitor_get_debug_mode(void)
40 {
41         return g_debug_mode;
42 }
43
44 void monitor_set_debug_mode(gboolean on)
45 {
46         g_debug_mode = on;
47 }
48
49 static gboolean dbus_cb_monitor_set_debug(SystemPassMonitor *obj,
50                 GDBusMethodInvocation *invoc,
51                 gboolean on,
52                 gpointer user_data)
53 {
54         if (monitor_get_debug_mode() != on) {
55                 monitor_set_debug_mode(on);
56                 _I("PASS Resource Monitor debug mode is set to %s", on ? "on" : "off");
57         }
58
59         g_dbus_method_invocation_return_value(invoc, g_variant_new("(i)", 0));
60
61         return TRUE;
62 }
63
64 static struct gdbus_signal_info g_gdbus_signal_infos[] = {
65         {
66                 .handler = DBUS_MONITOR_I_SET_DEBUG_HANDLER,
67                 .cb = G_CALLBACK(dbus_cb_monitor_set_debug),
68                 .cb_data = NULL,
69                 .ret_id = 0,
70         },
71 };
72
73 static int monitor_setup(void *data, void *user_data)
74 {
75         int ret;
76
77         syscommon_resman_init_resource_drivers();
78
79         ret = request_server_init();
80         if (ret < 0) {
81                 _E("failed to initialize request server\n");
82                 return ret;
83         }
84
85         ret = monitor_thread_init(&g_monitor);
86         if (ret < 0) {
87                 _E("failed to initialize monitor thread\n");
88                 request_server_exit();
89                 return ret;
90         }
91
92         return 0;
93 }
94
95 static void monitor_init(void *data)
96 {
97         /* This is the case of daemon creation: DO NOTHING */
98 }
99
100 static void monitor_exit(void *data)
101 {
102         monitor_thread_exit(&g_monitor);
103         request_server_exit();
104         syscommon_resman_exit_resource_drivers();
105         unregister_notifier(DEVICE_NOTIFIER_INIT_DONE, monitor_setup, NULL);
106 }
107
108 static int monitor_probe(void *data)
109 {
110         int ret = 0;
111
112         g_gdbus_instance = gdbus_get_instance_monitor();
113         if (g_gdbus_instance == NULL) {
114                 _E("failed to get instance for the %s interface\n",
115                                 DBUS_MONITOR_INTERFACE);
116                 return -EINVAL;
117         }
118
119         ret = gdbus_connect_signal(g_gdbus_instance,
120                         ARRAY_SIZE(g_gdbus_signal_infos), g_gdbus_signal_infos);
121         if (ret < 0) {
122                 _E("failed to register dbus methods\n");
123                 ret = -EINVAL;
124                 goto out;
125         }
126
127         ret = gdbus_export_interface(PASS_DBUS_CORE,
128                         g_gdbus_instance, DBUS_MONITOR_PATH);
129         if (ret < 0) {
130                 _E("cannot export the dbus interface '%s' at the object path '%s'\n",
131                                 DBUS_MONITOR_INTERFACE,
132                                 DBUS_MONITOR_PATH);
133                 ret = -EINVAL;
134                 goto out_disconnect;
135         }
136
137         /*
138          * Register a notifier for the booting-done event. The actual
139          * initialization of the daemon is performed by this notifier after
140          * booting is completely done.
141          */
142         ret = register_notifier(DEVICE_NOTIFIER_INIT_DONE,
143                         monitor_setup, NULL);
144         if (ret < 0) {
145                 _E("cannot register a callback function \
146                                 for the booting-done event (%d)\n", ret);
147                 goto out_disconnect;
148         }
149
150         return 0;
151
152 out_disconnect:
153         gdbus_disconnect_signal(g_gdbus_instance,
154                         ARRAY_SIZE(g_gdbus_signal_infos), g_gdbus_signal_infos);
155 out:
156         gdbus_put_instance_monitor(&g_gdbus_instance);
157
158         return ret;
159
160 }
161
162 static const struct device_ops monitor_device_ops = {
163         .name   = "monitor",
164         .probe  = monitor_probe,
165         .init   = monitor_init,
166         .exit   = monitor_exit,
167 };
168
169 DEVICE_OPS_REGISTER(&monitor_device_ops)