]> git.ozlabs.org Git - petitboot/commitdiff
Use a list for device->boot_options
authorJeremy Kerr <jk@ozlabs.org>
Fri, 2 Jan 2009 06:30:29 +0000 (15:30 +0900)
committerJeremy Kerr <jk@ozlabs.org>
Fri, 2 Jan 2009 06:30:29 +0000 (15:30 +0900)
Makes adding and removing options easier for parsers.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
discover/device-handler.c
lib/pb-protocol/pb-protocol.c
lib/pb-protocol/pb-protocol.h
rules.mk
ui/test/pb-test.c

index 98194ad46cea86434b9fdf8217b8876106cbbe16..1f8938f295b322e68b2df19fb7eeadd8146b1ca1 100644 (file)
@@ -61,8 +61,6 @@ static struct device device = {
        .name = "meep",
        .description = "meep description",
        .icon_file = "meep.png",
-       .n_options = 1,
-       .options = options,
 };
 
 int device_handler_get_current_devices(struct device_handler *handler,
@@ -378,6 +376,7 @@ int device_handler_event(struct device_handler *handler,
 struct device_handler *device_handler_init(struct discover_server *server)
 {
        struct device_handler *handler;
+       int i;
 
        handler = talloc(NULL, struct device_handler);
        handler->devices = NULL;
@@ -388,6 +387,12 @@ struct device_handler *device_handler_init(struct discover_server *server)
        /* set up our mount point base */
        mkdir_recursive(mount_base());
 
+       /* setup out test objects */
+       list_init(&device.boot_options);
+
+       for (i = 0; i < sizeof(options) / sizeof(options[0]); i++)
+               list_add(&device.boot_options, &options[i].list);
+
        return handler;
 }
 
index 2fd76c5dd10a78f5fcbab5d6fb83dbd6035b0ea0..ffa2c1b9b87f80b1b4077e6e2bf7135d7b2b5d69 100644 (file)
@@ -4,6 +4,7 @@
 #include <asm/byteorder.h>
 
 #include <talloc/talloc.h>
+#include <list/list.h>
 
 #include "pb-protocol.h"
 
@@ -115,7 +116,8 @@ static int optional_strlen(const char *str)
 
 int pb_protocol_device_len(struct device *dev)
 {
-       int len, i;
+       struct boot_option *opt;
+       int len;
 
        len =   4 + optional_strlen(dev->id) +
                4 + optional_strlen(dev->name) +
@@ -123,8 +125,7 @@ int pb_protocol_device_len(struct device *dev)
                4 + optional_strlen(dev->icon_file) +
                4;
 
-       for (i = 0; i < dev->n_options; i++) {
-               struct boot_option *opt = &dev->options[i];
+       list_for_each_entry(&dev->boot_options, opt, list) {
                len +=  4 + optional_strlen(opt->id) +
                        4 + optional_strlen(opt->name) +
                        4 + optional_strlen(opt->description) +
@@ -139,8 +140,9 @@ int pb_protocol_device_len(struct device *dev)
 
 int pb_protocol_serialise_device(struct device *dev, char *buf, int buf_len)
 {
+       struct boot_option *opt;
+       uint32_t n;
        char *pos;
-       int i;
 
        pos = buf;
 
@@ -151,12 +153,16 @@ int pb_protocol_serialise_device(struct device *dev, char *buf, int buf_len)
        pos += pb_protocol_serialise_string(pos, dev->icon_file);
 
        /* write option count */
-       *(uint32_t *)pos = __cpu_to_be32(dev->n_options);
+       n = 0;
+
+       list_for_each_entry(&dev->boot_options, opt, list)
+               n++;
+
+       *(uint32_t *)pos = __cpu_to_be32(n);
        pos += sizeof(uint32_t);
 
        /* write each option */
-       for (i = 0; i < dev->n_options; i++) {
-               struct boot_option *opt = &dev->options[i];
+       list_for_each_entry(&dev->boot_options, opt, list) {
                pos += pb_protocol_serialise_string(pos, opt->id);
                pos += pb_protocol_serialise_string(pos, opt->name);
                pos += pb_protocol_serialise_string(pos, opt->description);
@@ -252,7 +258,7 @@ struct device *pb_protocol_deserialise_device(void *ctx,
 {
        struct device *dev;
        char *pos;
-       int i, len;
+       int i, n_options, len;
 
        len = message->payload_len;
        pos = message->payload;
@@ -271,12 +277,15 @@ struct device *pb_protocol_deserialise_device(void *ctx,
        if (read_string(dev, &pos, &len, &dev->icon_file))
                goto out_err;
 
-       dev->n_options = __be32_to_cpu(*(uint32_t *)pos);
-       dev->options = talloc_array(dev, struct boot_option, dev->n_options);
+       n_options = __be32_to_cpu(*(uint32_t *)pos);
        pos += sizeof(uint32_t);
 
-       for (i = 0; i < dev->n_options; i++) {
-               struct boot_option *opt = &dev->options[i];
+       list_init(&dev->boot_options);
+
+       for (i = 0; i < n_options; i++) {
+               struct boot_option *opt;
+
+               opt = talloc(dev, struct boot_option);
 
                if (read_string(opt, &pos, &len, &opt->id))
                        goto out_err;
@@ -297,6 +306,8 @@ struct device *pb_protocol_deserialise_device(void *ctx,
                if (read_string(opt, &pos, &len,
                                        &opt->boot_args))
                        goto out_err;
+
+               list_add(&dev->boot_options, &opt->list);
        }
 
        return dev;
index 7b557d667c206cdc71ab69c5bf875048fa7c869b..ce9c96b42cb68090d2a011e3e518aa0a3d146b5c 100644 (file)
@@ -3,6 +3,8 @@
 
 #include <stdint.h>
 
+#include <list/list.h>
+
 #define PB_SOCKET_PATH "/tmp/petitboot.ui"
 
 #define PB_PROTOCOL_MAX_PAYLOAD_SIZE 4096
@@ -24,16 +26,19 @@ struct device {
        char *description;
        char *icon_file;
 
-       struct boot_option {
-               char *id;
-               char *name;
-               char *description;
-               char *icon_file;
-               char *boot_image_file;
-               char *initrd_file;
-               char *boot_args;
-       } *options;
-       int n_options;
+       struct list boot_options;
+};
+
+struct boot_option {
+       char *id;
+       char *name;
+       char *description;
+       char *icon_file;
+       char *boot_image_file;
+       char *initrd_file;
+       char *boot_args;
+
+       struct list_item list;
 };
 
 int pb_protocol_device_len(struct device *dev);
index 8ccdc5d955456e604fa363c43afa4e57f28a7b4d..3a343c14e96d2e64736541455de125f61cc9b898 100644 (file)
--- a/rules.mk
+++ b/rules.mk
@@ -32,7 +32,7 @@ ui/twin/pb-twin: $(pb_twin_objs)
 
 # test ui
 pb_test_objs = ui/test/pb-test.o ui/common/discover-client.o \
-       $(talloc_objs) $(server_objs)
+       $(talloc_objs) $(server_objs) $(list_objs)
 
 ui/test/pb-test: $(pb_test_objs)
        $(LINK.o) -o $@ $^
index e8004981b3fecf32e62e6a79d0a83d08ced6a77f..5f021ff7e73d73f6455c8855f5500a40f31a34ea 100644 (file)
@@ -5,7 +5,7 @@
 
 static int print_device_add(struct device *device)
 {
-       int i;
+       struct boot_option *opt;
 
        printf("new device:\n");
        printf("\tid:   %s\n", device->id);
@@ -13,9 +13,8 @@ static int print_device_add(struct device *device)
        printf("\tdesc: %s\n", device->description);
        printf("\ticon: %s\n", device->icon_file);
 
-       printf("\t%d boot options:\n", device->n_options);
-       for (i = 0; i < device->n_options; i++) {
-               struct boot_option *opt = &device->options[i];
+       printf("\tboot options:\n");
+       list_for_each_entry(&device->boot_options, opt, list) {
                printf("\t\tid:   %s\n", opt->id);
                printf("\t\tname: %s\n", opt->name);
                printf("\t\tdesc: %s\n", opt->description);