From 12a5c9153ce95ddde7fb14eaba9d779933a3637c Mon Sep 17 00:00:00 2001 From: Jeremy Kerr Date: Mon, 11 Mar 2013 14:07:36 +0800 Subject: [PATCH] discover: Consolidate user events by device ID Currently, we assume all user events are for a new device. This means that we can never add boot options to an existing device. This change tries to find an existing (matching by ID) device before creating a new one in the user event add path. Signed-off-by: Jeremy Kerr --- discover/device-handler.c | 24 +++++++++++++----------- discover/event-parser.c | 6 +++--- discover/parser.h | 3 ++- ui/test/discover-test.c | 18 ++++++++++++++++++ 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/discover/device-handler.c b/discover/device-handler.c index ab27b51..4ba7405 100644 --- a/discover/device-handler.c +++ b/discover/device-handler.c @@ -347,23 +347,25 @@ static int handle_add_user_event(struct device_handler *handler, assert(event->device); - device = talloc_zero(handler, struct device); + device = device_handler_find(handler, event->device); - if (!device) - goto fail; + if (!device) { + device = talloc_zero(handler, struct device); - device->id = talloc_strdup(device, event->device); - list_init(&device->boot_options); + if (!device) + goto fail; - parse_user_event(device, event); + device->id = talloc_strdup(device, event->device); + list_init(&device->boot_options); - discover_server_notify_device_add(handler->server, device); + /* add device to handler device array */ + device_handler_add(handler, device); - list_for_each_entry(&device->boot_options, opt, list) - discover_server_notify_boot_option_add(handler->server, opt); + discover_server_notify_device_add(handler->server, device); + } - /* add device to handler device array */ - device_handler_add(handler, device); + opt = parse_user_event(device, event); + discover_server_notify_boot_option_add(handler->server, opt); return 0; diff --git a/discover/event-parser.c b/discover/event-parser.c index 1eec5c9..c09c5ba 100644 --- a/discover/event-parser.c +++ b/discover/event-parser.c @@ -13,7 +13,7 @@ * Understands params: name, image, args. */ -int parse_user_event(struct device *device, struct event *event) +struct boot_option *parse_user_event(struct device *device, struct event *event) { struct boot_option *opt; const char *p; @@ -54,9 +54,9 @@ int parse_user_event(struct device *device, struct event *event) device_add_boot_option(device, opt); - return 0; + return opt; fail: talloc_free(opt); - return -1; + return NULL; } diff --git a/discover/parser.h b/discover/parser.h index 589c6ff..a80d1b5 100644 --- a/discover/parser.h +++ b/discover/parser.h @@ -21,6 +21,7 @@ enum generic_icon_type { void parser_init(void); void iterate_parsers(struct discover_context *ctx); -int parse_user_event(struct device *device, struct event *event); +struct boot_option *parse_user_event(struct device *device, + struct event *event); #endif /* _PARSER_H */ diff --git a/ui/test/discover-test.c b/ui/test/discover-test.c index ae43b1e..8f7c2c2 100644 --- a/ui/test/discover-test.c +++ b/ui/test/discover-test.c @@ -28,6 +28,23 @@ static int print_device_add(struct device *device, return 0; } +static int print_boot_option_add(struct device *dev, + struct boot_option *opt, + void __attribute__((unused)) *arg) +{ + printf("new boot option (dev: %s):\n", dev->id); + printf("\tdev id: %s\n", opt->device_id); + printf("\tid: %s\n", opt->id); + printf("\tname: %s\n", opt->name); + printf("\tdesc: %s\n", opt->description); + printf("\ticon: %s\n", opt->icon_file); + printf("\tboot: %s\n", opt->boot_image_file); + printf("\tinit: %s\n", opt->initrd_file); + printf("\targs: %s\n", opt->boot_args); + + return 0; +} + static void print_device_remove(struct device *device, void __attribute__((unused)) *arg) { @@ -38,6 +55,7 @@ static void print_device_remove(struct device *device, static struct discover_client_ops client_ops = { .device_add = print_device_add, + .boot_option_add = print_boot_option_add, .device_remove = print_device_remove, }; -- 2.39.2