diff options
author | Jeremy Kerr <jk@ozlabs.org> | 2013-03-05 13:49:15 +0800 |
---|---|---|
committer | Geoff Levand <geoff@infradead.org> | 2013-03-05 06:06:17 -0800 |
commit | 0fdc5cc3f296db134729fe58e074c20084b99459 (patch) | |
tree | 097c3d31633d6588416fd56873ab869633ff23e6 | |
parent | 6d51866f409e90274ff8cfb06b92a6c828692698 (diff) | |
download | talos-petitboot-0fdc5cc3f296db134729fe58e074c20084b99459.tar.gz talos-petitboot-0fdc5cc3f296db134729fe58e074c20084b99459.zip |
discover: simplify handler switch statement
Instead of using nested switch statements in device_handler_event,
define an array of handlers, and lookup instead.
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Signed-off-by: Geoff Levand <geoff@infradead.org>
-rw-r--r-- | discover/device-handler.c | 55 | ||||
-rw-r--r-- | discover/event.h | 2 |
2 files changed, 22 insertions, 35 deletions
diff --git a/discover/device-handler.c b/discover/device-handler.c index 6e03ef3..e65eb61 100644 --- a/discover/device-handler.c +++ b/discover/device-handler.c @@ -387,46 +387,31 @@ static int handle_remove_user_event(struct device_handler *handler, return 0; } +typedef int (*event_handler)(struct device_handler *, struct event *); + +static event_handler handlers[EVENT_TYPE_MAX][EVENT_ACTION_MAX] = { + [EVENT_TYPE_UDEV] = { + [EVENT_ACTION_ADD] = handle_add_udev_event, + [EVENT_ACTION_REMOVE] = handle_remove_udev_event, + }, + [EVENT_TYPE_USER] = { + [EVENT_ACTION_ADD] = handle_add_user_event, + [EVENT_ACTION_REMOVE] = handle_remove_user_event, + } +}; + int device_handler_event(struct device_handler *handler, struct event *event) { - int rc = 0; - - switch (event->type) { - case EVENT_TYPE_UDEV: - switch (event->action) { - case EVENT_ACTION_ADD: - rc = handle_add_udev_event(handler, event); - break; - case EVENT_ACTION_REMOVE: - rc = handle_remove_udev_event(handler, event); - break; - default: - pb_log("%s unknown action: %d\n", __func__, - event->action); - break; - } - break; - case EVENT_TYPE_USER: - switch (event->action) { - case EVENT_ACTION_ADD: - rc = handle_add_user_event(handler, event); - break; - case EVENT_ACTION_REMOVE: - rc = handle_remove_user_event(handler, event); - break; - default: - pb_log("%s unknown action: %d\n", __func__, - event->action); - break; - } - break; - default: - pb_log("%s unknown type: %d\n", __func__, event->type); - break; + if (event->type >= EVENT_TYPE_MAX || + event->action >= EVENT_ACTION_MAX || + !handlers[event->type][event->action]) { + pb_log("%s unknown type/action: %d/%d\n", __func__, + event->type, event->action); + return 0; } - return rc; + return handlers[event->type][event->action](handler, event); } struct device_handler *device_handler_init(struct discover_server *server) diff --git a/discover/event.h b/discover/event.h index 20585f2..91575cc 100644 --- a/discover/event.h +++ b/discover/event.h @@ -4,11 +4,13 @@ enum event_type { EVENT_TYPE_UDEV = 10, EVENT_TYPE_USER, + EVENT_TYPE_MAX, }; enum event_action { EVENT_ACTION_ADD = 20, EVENT_ACTION_REMOVE, + EVENT_ACTION_MAX, }; struct event { |