aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTakashi Sakamoto <o-takashi@sakamocchi.jp>2022-05-05 16:16:39 +0900
committer坂本 貴史 <o-takashi@sakamocchi.jp>2022-05-05 20:51:53 +0900
commitd9e8b0724cd688a5fc7c62c8b64bc32df9e530b7 (patch)
tree429d800313a49502010be116043fc8cbd77e5a24
parent52977cfd1817ab3468b3966432826d5173286e65 (diff)
downloadlibhinoko-d9e8b0724cd688a5fc7c62c8b64bc32df9e530b7.tar.gz
fw_iso_resource: code refactoring to handle several types of event
It's planned to support event of bus reset to detect generation. This commit is a preparation for it so that object class implements interface is allowed to handle several kind of events. Signed-off-by: Takashi Sakamoto <o-takashi@sakamocchi.jp>
-rw-r--r--src/fw_iso_resource_auto.c58
-rw-r--r--src/fw_iso_resource_once.c33
-rw-r--r--src/fw_iso_resource_private.c62
-rw-r--r--src/fw_iso_resource_private.h6
4 files changed, 102 insertions, 57 deletions
diff --git a/src/fw_iso_resource_auto.c b/src/fw_iso_resource_auto.c
index ec4f8cd..3a1357c 100644
--- a/src/fw_iso_resource_auto.c
+++ b/src/fw_iso_resource_auto.c
@@ -144,35 +144,61 @@ static gboolean fw_iso_resource_auto_open(HinokoFwIsoResource *inst, const gchar
return fw_iso_resource_open(&priv->fd, path, open_flag, error);
}
-void fw_iso_resource_auto_handle_event(HinokoFwIsoResource *inst, const char *signal_name,
- guint channel, guint bandwidth, const GError *error)
+static void handle_iso_resource_event(HinokoFwIsoResourceAuto *self,
+ const struct fw_cdev_event_iso_resource *ev)
{
- HinokoFwIsoResourceAuto *self;
- HinokoFwIsoResourceAutoPrivate *priv;
+ const char *signal_name;
+ guint channel;
+ guint bandwidth;
+ GError *error;
- g_return_if_fail(HINOKO_IS_FW_ISO_RESOURCE_AUTO(inst));
- self = HINOKO_FW_ISO_RESOURCE_AUTO(inst);
- priv = hinoko_fw_iso_resource_auto_get_instance_private(self);
+ parse_iso_resource_event(ev, &channel, &bandwidth, &signal_name, &error);
- if (!strcmp(signal_name, ALLOCATED_SIGNAL_NAME)) {
- if (error == NULL) {
- g_mutex_lock(&priv->mutex);
+ if (error == NULL) {
+ HinokoFwIsoResourceAutoPrivate *priv =
+ hinoko_fw_iso_resource_auto_get_instance_private(self);
+
+ g_mutex_lock(&priv->mutex);
+
+ switch (ev->type) {
+ case FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED:
priv->channel = channel;
priv->bandwidth = bandwidth;
priv->is_allocated = TRUE;
- g_mutex_unlock(&priv->mutex);
- }
- } else {
- if (error == NULL) {
- g_mutex_lock(&priv->mutex);
+ break;
+ case FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED:
priv->channel = 0;
priv->bandwidth -= bandwidth;
priv->is_allocated = FALSE;
- g_mutex_unlock(&priv->mutex);
+ break;
+ default:
+ break;
}
+
+ g_mutex_unlock(&priv->mutex);
}
g_signal_emit_by_name(self, signal_name, channel, bandwidth, error);
+
+ if (error != NULL)
+ g_clear_error(&error);
+}
+
+void fw_iso_resource_auto_handle_event(HinokoFwIsoResource *inst, const union fw_cdev_event *event)
+{
+ HinokoFwIsoResourceAuto *self;
+
+ g_return_if_fail(HINOKO_IS_FW_ISO_RESOURCE_AUTO(inst));
+ self = HINOKO_FW_ISO_RESOURCE_AUTO(inst);
+
+ switch (event->common.type) {
+ case FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED:
+ case FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED:
+ handle_iso_resource_event(self, &event->iso_resource);
+ break;
+ default:
+ break;
+ }
}
static gboolean fw_iso_resource_auto_create_source(HinokoFwIsoResource *inst, GSource **source,
diff --git a/src/fw_iso_resource_once.c b/src/fw_iso_resource_once.c
index 0983c19..62b7744 100644
--- a/src/fw_iso_resource_once.c
+++ b/src/fw_iso_resource_once.c
@@ -60,10 +60,37 @@ static gboolean fw_iso_resource_once_open(HinokoFwIsoResource *inst, const gchar
return fw_iso_resource_open(&priv->fd, path, open_flag, error);
}
-void fw_iso_resource_once_handle_event(HinokoFwIsoResource *inst, const char *signal_name,
- guint channel, guint bandwidth, const GError *error)
+static void handle_iso_resource_event(HinokoFwIsoResourceOnce *self,
+ const struct fw_cdev_event_iso_resource *ev)
{
- g_signal_emit_by_name(inst, signal_name, channel, bandwidth, error);
+ const char *signal_name;
+ guint channel;
+ guint bandwidth;
+ GError *error;
+
+ parse_iso_resource_event(ev, &channel, &bandwidth, &signal_name, &error);
+
+ g_signal_emit_by_name(self, signal_name, channel, bandwidth, error);
+
+ if (error != NULL)
+ g_clear_error(&error);
+}
+
+void fw_iso_resource_once_handle_event(HinokoFwIsoResource *inst, const union fw_cdev_event *event)
+{
+ HinokoFwIsoResourceOnce *self;
+
+ g_return_if_fail(HINOKO_IS_FW_ISO_RESOURCE_ONCE(inst));
+ self = HINOKO_FW_ISO_RESOURCE_ONCE(inst);
+
+ switch (event->common.type) {
+ case FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED:
+ case FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED:
+ handle_iso_resource_event(self, &event->iso_resource);
+ break;
+ default:
+ break;
+ }
}
static gboolean fw_iso_resource_once_create_source(HinokoFwIsoResource *inst, GSource **source,
diff --git a/src/fw_iso_resource_private.c b/src/fw_iso_resource_private.c
index a2d6fc3..44f5e88 100644
--- a/src/fw_iso_resource_private.c
+++ b/src/fw_iso_resource_private.c
@@ -17,14 +17,14 @@ static const char *const err_msgs[] = {
#define generate_local_error(error, code) \
g_set_error_literal(error, HINOKO_FW_ISO_RESOURCE_ERROR, code, err_msgs[code])
+#define generate_file_error(error, code, format, arg) \
+ g_set_error(error, G_FILE_ERROR, code, format, arg)
+
#define generate_event_error(error, errno) \
g_set_error(error, HINOKO_FW_ISO_RESOURCE_ERROR, \
HINOKO_FW_ISO_RESOURCE_ERROR_EVENT, \
"%d %s", errno, strerror(errno))
-#define generate_file_error(error, code, format, arg) \
- g_set_error(error, G_FILE_ERROR, code, format, arg)
-
typedef struct {
GSource src;
HinokoFwIsoResource *self;
@@ -32,8 +32,7 @@ typedef struct {
gsize len;
guint8 *buf;
int fd;
- void (*handle_event)(HinokoFwIsoResource *self, const char *signal_name, guint channel,
- guint bandwidth, const GError *error);
+ void (*handle_event)(HinokoFwIsoResource *self, const union fw_cdev_event *event);
} FwIsoResourceSource;
gboolean fw_iso_resource_open(int *fd, const gchar *path, gint open_flag, GError **error)
@@ -61,34 +60,6 @@ gboolean fw_iso_resource_open(int *fd, const gchar *path, gint open_flag, GError
return TRUE;
}
-static void handle_iso_resource_event(FwIsoResourceSource *src,
- const struct fw_cdev_event_iso_resource *ev)
-{
- guint channel;
- guint bandwidth;
- const char *signal_name;
- GError *error = NULL;
-
- if (ev->channel >= 0) {
- channel = ev->channel;
- bandwidth = ev->bandwidth;
- } else {
- channel = 0;
- bandwidth = 0;
- generate_event_error(&error, -ev->channel);
- }
-
- if (ev->type == FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED)
- signal_name = ALLOCATED_SIGNAL_NAME;
- else
- signal_name = DEALLOCATED_SIGNAL_NAME;
-
- src->handle_event(src->self, signal_name, channel, bandwidth, error);
-
- if (error != NULL)
- g_clear_error(&error);
-}
-
static gboolean check_src(GSource *source)
{
FwIsoResourceSource *src = (FwIsoResourceSource *)source;
@@ -126,7 +97,7 @@ static gboolean dispatch_src(GSource *source, GSourceFunc cb, gpointer user_data
switch (ev->common.type) {
case FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED:
case FW_CDEV_EVENT_ISO_RESOURCE_DEALLOCATED:
- handle_iso_resource_event(src, &ev->iso_resource);
+ src->handle_event(src->self, ev);
break;
default:
break;
@@ -146,8 +117,7 @@ static void finalize_src(GSource *source)
gboolean fw_iso_resource_create_source(int fd, HinokoFwIsoResource *inst,
void (*handle_event)(HinokoFwIsoResource *self,
- const char *signal_name, guint channel,
- guint bandwidth, const GError *error),
+ const union fw_cdev_event *event),
GSource **source, GError **error)
{
static GSourceFuncs funcs = {
@@ -224,3 +194,23 @@ void fw_iso_resource_waiter_wait(struct fw_iso_resource_waiter *w, HinokoFwIsoRe
else if (w->error != NULL)
*error = w->error; // Delegate ownership.
}
+
+void parse_iso_resource_event(const struct fw_cdev_event_iso_resource *ev, guint *channel,
+ guint *bandwidth, const char **signal_name, GError **error)
+{
+ *error = NULL;
+
+ if (ev->channel >= 0) {
+ *channel = ev->channel;
+ *bandwidth = ev->bandwidth;
+ } else {
+ *channel = 0;
+ *bandwidth = 0;
+ generate_event_error(error, -ev->channel);
+ }
+
+ if (ev->type == FW_CDEV_EVENT_ISO_RESOURCE_ALLOCATED)
+ *signal_name = ALLOCATED_SIGNAL_NAME;
+ else
+ *signal_name = DEALLOCATED_SIGNAL_NAME;
+}
diff --git a/src/fw_iso_resource_private.h b/src/fw_iso_resource_private.h
index c839407..db91413 100644
--- a/src/fw_iso_resource_private.h
+++ b/src/fw_iso_resource_private.h
@@ -20,8 +20,7 @@ gboolean fw_iso_resource_open(int *fd, const gchar *path, gint open_flag, GError
gboolean fw_iso_resource_create_source(int fd, HinokoFwIsoResource *inst,
void (*handle_event)(HinokoFwIsoResource *self,
- const char *signal_name, guint channel,
- guint bandwidth, const GError *error),
+ const union fw_cdev_event *event),
GSource **source, GError **error);
struct fw_iso_resource_waiter {
@@ -39,4 +38,7 @@ void fw_iso_resource_waiter_init(struct fw_iso_resource_waiter *w, HinokoFwIsoRe
void fw_iso_resource_waiter_wait(struct fw_iso_resource_waiter *w, HinokoFwIsoResource *self,
GError **error);
+void parse_iso_resource_event(const struct fw_cdev_event_iso_resource *ev, guint *channel,
+ guint *bandwidth, const char **signal_name, GError **error);
+
#endif