* @ingroup COM_POWER_MGNT
*/
-#include <glib-unix.h>
-#include <libudev.h>
-
#include <util/log.h>
+#include <util/timer.h>
#include "pass.h"
#include "pass-hal.h"
struct pass_resmon *resmon = monitor->resmon;
int ret;
- /* Add new timer-based resmon to timer_list */
- if (!resmon_find_monitor(resmon, RESMON_TIMER, monitor->id)) {
- monitor->id = g_timeout_add((guint)interval,
- (GSourceFunc)resmon_timer_func,
- (gpointer)monitor);
- if (!monitor->id)
- return -EPERM;
- monitor->timer_interval = interval;
-
- if (monitor->ops && monitor->ops->init) {
- ret = monitor->ops->init(monitor);
- if (ret < 0) {
- g_source_remove(monitor->id);
- monitor->timer_interval = 0;
- return ret;
- }
+ if (resmon_find_monitor(resmon, RESMON_TIMER, monitor->id))
+ return -EINVAL;
+
+ monitor->id = add_timer_handler(interval, resmon_timer_func, monitor);
+ if (!monitor->id)
+ return -EPERM;
+ monitor->timer_interval = interval;
+
+ if (monitor->ops && monitor->ops->init) {
+ ret = monitor->ops->init(monitor);
+ if (ret < 0) {
+ delete_timer_handler(monitor->id);
+ monitor->timer_interval = 0;
+ return ret;
}
+ }
- resmon->timer_list = g_list_append(resmon->timer_list,
- (gpointer)monitor);
+ resmon->timer_list = g_list_append(resmon->timer_list, (gpointer)monitor);
- /* Update timer interval of the registered timer-based resmon */
- } else {
- g_source_remove(monitor->id);
- monitor->timer_interval = 0;
+ return monitor->id;
+}
- /* Update timer interval of timer-based resmon */
- monitor->id = g_timeout_add((guint)interval,
- (GSourceFunc)resmon_timer_func,
- (gpointer)monitor);
- if (!monitor->id)
- return -EPERM;
- monitor->timer_interval = interval;
+/**
+ * @brief Update the interval of a timer-based resource monitor.
+ * @param [in] monitor Instance of a resource monitor
+ * @param [in] interval New interval of timer-based resource monitor
+ * @return @c 0 on success, otherwise error value
+ */
+static int resmon_timer_update_interval(struct resmon *monitor,
+ unsigned int interval)
+{
+ struct pass_resmon *resmon = monitor->resmon;
+
+ if (!resmon_find_monitor(resmon, RESMON_TIMER, monitor->id))
+ return -EINVAL;
+
+ /* Update timer interval of timer-based resmon */
+ delete_timer_handler(monitor->id);
+
+ monitor->id = add_timer_handler(interval, resmon_timer_func, monitor);
+ if (!monitor->id) {
+ _E("failed to update interval of timer (id:%d)\n\n", monitor->id);
+ return -EPERM;
}
+ monitor->timer_interval = interval;
- return monitor->id;
+ return 0;
}
/**
struct pass_resmon *resmon = monitor->resmon;
int ret;
- g_source_remove(monitor->id);
+ delete_timer_handler(monitor->id);
if (monitor->ops && monitor->ops->exit) {
ret = monitor->ops->exit(monitor);
return 0;
/* Update new interval of timer-based resmon */
- ret = resmon_timer_add(monitor, timer_interval);
+ ret = resmon_timer_update_interval(monitor, timer_interval);
if (ret < 0)
return -EINVAL;
struct pass_resmon *resmon = monitor->resmon;
struct pass_resource *res
= container_of(resmon, struct pass_resource, resmon);;
- guint gfd;
int ret;
- int fd;
- int i;
if (!monitor->ops) {
_E("failed to add the udev-monitor\n");
return -EINVAL;
}
- /* Initialize the udev-monitor instance and set buffer size */
- udev_monitor = udev_monitor_new_from_netlink(g_udev, "kernel");
+ udev_monitor = add_uevent_handler(monitor->ops->uevent_subsystem,
+ monitor->ops->uevent_devtype,
+ monitor->ops->number_of_uevent,
+ resmon_uevent_func,
+ (void *)monitor);
if (!udev_monitor) {
- _E("failed to create the udev monitor " \
- "(res_name:%s, src_type: 0x%x)\n",
- res->config_data.res_name, monitor->src_type);
+ _E("failed to add uevent handler\n");
return -EINVAL;
}
/*
- * Number of uevent subsystem must be more than 0 at least.
- */
- if (monitor->ops->number_of_uevent <= 0) {
- _E("failed to add filter due to subsystem/devtype are NULL " \
- "(res_name:%s, src_type: 0x%x)\n",
- res->config_data.res_name, monitor->src_type);
- ret = -EINVAL;
- goto err_udev_monitor;
- }
-
- /* Update the kernel's subsystem and devtype for filtering */
- for (i = 0; i < monitor->ops->number_of_uevent; i++) {
- ret = udev_monitor_filter_add_match_subsystem_devtype(
- udev_monitor,
- monitor->ops->uevent_subsystem[i],
- monitor->ops->uevent_devtype[i]);
- if (ret < 0) {
- _E("failed to add filter with subsystem(%s)/devtype(%s) " \
- "(res_name:%s, src_type: 0x%x)\n",
- monitor->ops->uevent_subsystem[i],
- monitor->ops->uevent_devtype[i],
- res->config_data.res_name, monitor->src_type);
- goto err_udev_monitor;
- }
- }
-
- ret = udev_monitor_filter_update(udev_monitor);
- if (ret < 0) {
- for (i = 0; i < monitor->ops->number_of_uevent; i++) {
- _E("failed to update filter with subsystem(%s)/devtype(%s) " \
- "(res_name:%s, src_type: 0x%x)\n",
- monitor->ops->uevent_subsystem[i],
- monitor->ops->uevent_devtype[i],
- res->config_data.res_name, monitor->src_type);
- }
- goto err_udev_monitor;
- }
-
- /* Register callback to subscribe the event */
- fd = udev_monitor_get_fd(udev_monitor);
- if (fd <= 0) {
- _E("failed to get file descriptor of udev monitor " \
- "(res_name:%s, src_type: 0x%x)\n",
- res->config_data.res_name, monitor->src_type);
- ret = -EINVAL;
- goto err_udev_monitor;
- }
-
- gfd = g_unix_fd_add(fd, G_IO_IN, resmon_uevent_func, monitor);
- if (gfd <= 0) {
- _E("failed to add uevent-based callback function " \
- "(res_name:%s, src_type: 0x%x)\n",
- res->config_data.res_name, monitor->src_type);
- ret = -EINVAL;
- goto err_udev_monitor;
- }
-
- /*
* Invoke the .init of each RESMON_SRC_* source
* before enabling the udev monitoring.
*/
}
}
- /* Bind udev-monitor to enable the udev monitoring */
- ret = udev_monitor_enable_receiving(udev_monitor);
- if (ret < 0) {
- _E("failed to bind udev monitor for receiving the event " \
- "(res_name:%s, src_type: 0x%x)\n",
- res->config_data.res_name, monitor->src_type);
- ret = -EAGAIN;
- goto err_udev_monitor_fd;
- }
-
/* Add new uevent-based resmon to uevent_list */
resmon->uevent_list = g_list_append(resmon->uevent_list,
(gpointer)monitor);
monitor->udev_monitor = udev_monitor;;
- monitor->udev_monitor_fd = gfd;
monitor->id = ++uevent_id;
return 0;
err_udev_monitor_fd:
- g_source_remove(gfd);
- gfd = -1;
-err_udev_monitor:
udev_monitor_unref(udev_monitor);
udev_monitor = NULL;
}
}
- udev_monitor_unref(monitor->udev_monitor);
- g_source_remove(monitor->udev_monitor_fd);
+ delete_uevent_handler(monitor->udev_monitor);
monitor->udev_monitor = NULL;
- monitor->udev_monitor_fd = -1;
/* Delete the uevent-based resmon from uevent_list */
resmon->uevent_list = g_list_remove(resmon->uevent_list,