mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
To: barebox@lists.infradead.org
Subject: [PATCH 3/7] globalvar: allow to register multiple device
Date: Sat, 26 Jan 2013 00:55:28 +0100	[thread overview]
Message-ID: <1359158132-8023-3-git-send-email-plagnioj@jcrosoft.com> (raw)
In-Reply-To: <1359158132-8023-1-git-send-email-plagnioj@jcrosoft.com>

This will allow to reduce the time spend to search for globalvar
(boot time)

the access to the globalvar is retro-compatible

we can now create device via the global command

 barebox@Somfy Animeo IP:/
 # devinfo
 devices:
 `---- global
      `---- net
      `---- bootm
      `---- dhcp
      ...
 barebox@Somfy Animeo IP:/
 resources:
 driver: none
 bus: global
 bus: none

 Parameters:
         hostname =
 barebox@Somfy Animeo IP:/
 # devinfo net
 resources:
 driver: none
 bus: global

 Parameters:
       nameserver =
       domainname =
 barebox@Somfy Animeo IP:/
 # devinfo dhcp
 resources:
 driver: none
 bus: global

 Parameters:
         rootpath =
 tftp_server_name =
         bootfile =
      oftree_file =
        vendor_id = barebox-animeo-ip
        client_id =
       user_class =
      client_uuid =
 barebox@Somfy Animeo IP:/
 # devinfo bootm
 resources:
 driver: none
 bus: global

 Parameters:
            image =
           oftree =
           initrd =
Signed-off-by: Jean-Christophe PLAGNIOL-VILLARD <plagnioj@jcrosoft.com>
---
 commands/global.c   |   41 ++++++++++++----
 common/globalvar.c  |  129 +++++++++++++++++++++++++++++++++++++++++++++------
 include/globalvar.h |   26 +++++++++++
 3 files changed, 174 insertions(+), 22 deletions(-)

diff --git a/commands/global.c b/commands/global.c
index 427a231..539fa21 100644
--- a/commands/global.c
+++ b/commands/global.c
@@ -23,14 +23,23 @@
 #include <environment.h>
 #include <getopt.h>
 
-static int globalvar_set(char* name, char* value)
+static int globalvar_set(char *devname, char* name, char* value)
 {
 	int ret;
 
-	ret = globalvar_add_simple(name);
+	if (devname) {
+		struct device_d *dev;
+
+		dev = get_device_by_name(devname);
+		if (!dev)
+			dev = global_add_device(devname);
+		ret = global_add_simple(dev, name);
+	} else {
+		ret = globalvar_add_simple(name);
+	}
 
 	if (value) {
-		char *tmp = asprintf("global.%s", name);
+		char *tmp = asprintf("%s.%s", devname ? devname : "global", name);
 		ret = setenv(tmp, value);
 		free(tmp);
 	}
@@ -43,12 +52,16 @@ static int do_global(int argc, char *argv[])
 	int opt;
 	int do_set_match = 0;
 	char *value;
+	char *devname = NULL;
 
-	while ((opt = getopt(argc, argv, "r")) > 0) {
+	while ((opt = getopt(argc, argv, "rd:")) > 0) {
 		switch (opt) {
 		case 'r':
 			do_set_match = 1;
 			break;
+		case 'd':
+			devname = optarg;
+			break;
 		}
 	}
 
@@ -68,17 +81,29 @@ static int do_global(int argc, char *argv[])
 		if (!value)
 			value = "";
 
-		globalvar_set_match(argv[0], value);
+		if (devname) {
+			struct device_d *dev;
+
+			dev = get_device_by_name(devname);
+
+			if (!dev)
+				return -EINVAL;
+
+			global_set_match(dev, argv[0], value);
+		} else {
+			globalvar_set_match(argv[0], value);
+		}
 		return 0;
 	}
 
-	return globalvar_set(argv[0], value);
+	return globalvar_set(devname, argv[0], value);
 }
 
 BAREBOX_CMD_HELP_START(global)
-BAREBOX_CMD_HELP_USAGE("global [-r] <var>[=<value]\n")
+BAREBOX_CMD_HELP_USAGE("global [-d device] [-r] <var>[=<value]\n")
 BAREBOX_CMD_HELP_SHORT("add a new global variable named <var>, optionally set to <value>\n")
-BAREBOX_CMD_HELP_SHORT("-r to set a value to of all globalvars beginning with 'match'")
+BAREBOX_CMD_HELP_SHORT("-r to set a value to of all globalvars beginning with 'match'\n")
+BAREBOX_CMD_HELP_SHORT("-d use a specific global device if do not exist create (if -r not set), it if not set use 'global'")
 BAREBOX_CMD_HELP_END
 
 BAREBOX_CMD_START(global)
diff --git a/common/globalvar.c b/common/globalvar.c
index f275a38..d0aa2a3 100644
--- a/common/globalvar.c
+++ b/common/globalvar.c
@@ -14,19 +14,19 @@ int globalvar_add(const char *name,
 }
 
 /*
- * globalvar_get_match
+ * global_get_match
  *
- * get a concatenated string of all globalvars beginning with 'match'.
- * This adds whitespaces between the different globalvars
+ * get a concatenated string of all global vars beginning with 'match'.
+ * This adds whitespaces between the different global vars
  */
-char *globalvar_get_match(const char *match, const char *seperator)
+char *global_get_match(struct device_d *dev, const char *match, const char *seperator)
 {
 	char *val = NULL;
 	struct param_d *param;
 
-	list_for_each_entry(param, global_device.parameters, list) {
+	list_for_each_entry(param, &dev->parameters, list) {
 		if (!strncmp(match, param->name, strlen(match))) {
-			const char *p = dev_get_param(global_device, param->name);
+			const char *p = dev_get_param(dev, param->name);
 			if (val) {
 				char *new = asprintf("%s%s%s", val, seperator, p);
 				free(val);
@@ -43,14 +43,75 @@ char *globalvar_get_match(const char *match, const char *seperator)
 	return val;
 }
 
-void globalvar_set_match(const char *match, const char *val)
+/*
+ * globalvar_get_match
+ *
+ * get a concatenated string of all globalvars beginning with 'match'.
+ * This adds whitespaces between the different globalvars
+ */
+char *globalvar_get_match(const char *match, const char *seperator)
+{
+	return global_get_match(global_device, match, seperator);
+}
+
+void global_set_match(struct device_d *dev, const char *match, const char *val)
 {
 	struct param_d *param;
 
-	list_for_each_entry(param, global_device.parameters, list) {
+	list_for_each_entry(param, &dev->parameters, list) {
 		if (!strncmp(match, param->name, strlen(match)))
-			dev_set_param(global_device, param->name, val);
+			dev_set_param(dev, param->name, val);
+	}
+}
+
+void globalvar_set_match(const char *match, const char *val)
+{
+	struct device_d *child;
+	char *tmp = NULL;
+	char *submatch = "";
+
+	if (strchr(match, '.')) {
+		tmp = strdup(match);
+		submatch = strchr(tmp, '.');
+		*submatch = 0;
+		submatch++;
 	}
+
+	global_set_match(global_device, match, val);
+
+	device_for_each_child(global_device, child) {
+		const char *devname = dev_name(child);
+
+		if (tmp && !strcmp(devname, tmp))
+			global_set_match(child, submatch, val);
+
+		if (!strncmp(devname, match, strlen(match)))
+			global_set_match(child, "", val);
+	}
+
+	free(tmp);
+}
+
+/*
+ * global_add_simple
+ *
+ * add a new global named 'name'
+ */
+int global_add_simple(struct device_d *dev, const char *name)
+{
+	return dev_add_param(dev, name, NULL, NULL, 0);
+}
+
+static struct device_d *global_get_child_by_name(const char *name)
+{
+	struct device_d *child;
+
+	device_for_each_child(global_device, child) {
+		if (!strcmp(dev_name(child), name))
+			return child;
+	}
+
+	return NULL;
 }
 
 /*
@@ -60,7 +121,27 @@ void globalvar_set_match(const char *match, const char *val)
  */
 int globalvar_add_simple(const char *name)
 {
-	return globalvar_add(name, NULL, NULL, 0);
+	struct device_d *dev = global_device;
+	const char *subname = name;
+	char *tmp = NULL;
+	int ret;
+
+	if (strchr(name, '.')) {
+		char *data;
+
+		tmp = strdup(name);
+		data = strchr(tmp, '.');
+		*data = 0;
+		subname = ++data;
+
+		dev = global_get_child_by_name(tmp);
+	}
+
+	ret = global_add_simple(dev, subname);
+
+	free(tmp);
+
+	return ret;
 }
 
 static int global_match(struct device_d *dev, struct driver_d *drv)
@@ -74,11 +155,31 @@ static struct bus_type global_bus = {
 	.probe = dummy_probe,
 };
 
-static int globalvar_init(void)
+struct device_d *global_add_device(const char *name)
 {
-	bus_register(&global_bus);
-	global_device = &global_bus->dev;
+	struct device_d *dev;
+
+	dev = xzalloc(sizeof(struct device_d));
+
+	strcpy(dev->name, name);
+	dev->id = DEVICE_ID_SINGLE;
+	dev->bus = &global_bus;
+
+	register_device(dev);
+
+	return dev;
+}
+
+static int global_bus_init(void)
+{
+	int ret;
+
+	ret = bus_register(&global_bus);
+	if (ret)
+		return ret;
+
+	global_device = &global_bus.dev;
 
 	return 0;
 }
-pure_initcall(globalvar_init);
+pure_initcall(global_bus_init);
diff --git a/include/globalvar.h b/include/globalvar.h
index ddf885f..c1d73c4 100644
--- a/include/globalvar.h
+++ b/include/globalvar.h
@@ -2,6 +2,7 @@
 #define __GLOBALVAR_H
 
 #ifdef CONFIG_GLOBALVAR
+struct device_d *global_add_device(const char *name);
 int globalvar_add_simple(const char *name);
 
 int globalvar_add(const char *name,
@@ -10,6 +11,13 @@ int globalvar_add(const char *name,
 		unsigned long flags);
 char *globalvar_get_match(const char *match, const char *seperator);
 void globalvar_set_match(const char *match, const char *val);
+
+struct device_d *global_add_device(const char *name);
+int global_add_simple(struct device_d *dev, const char *name);
+void global_set_match(struct device_d *dev, const char *match,
+			const char *val);
+char *global_get_match(struct device_d *dev, const char *match,
+			const char *seperator);
 #else
 static inline int globalvar_add_simple(const char *name)
 {
@@ -30,6 +38,24 @@ static inline char *globalvar_get_match(const char *match, const char *seperator
 }
 
 static inline void globalvar_set_match(const char *match, const char *val) {}
+
+struct device_d *global_add_device(const char *name)
+{
+	return NULL;
+}
+
+int global_add_simple(struct device_d *dev, const char *name)
+{
+	return 0;
+}
+
+void global_set_match(struct device_d *dev, const char *match,
+			const char *val) {}
+char *global_get_match(struct device_d *dev, const char *match,
+			const char *seperator)
+{
+	return NULL;
+}
 #endif
 
 #endif /* __GLOBALVAR_H */
-- 
1.7.10.4


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2013-01-25 23:56 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-25 23:53 [PATCH 0/7] globalvar: add multiple device support Jean-Christophe PLAGNIOL-VILLARD
2013-01-25 23:55 ` [PATCH 1/7] params: allow to access first sub-device and params via env Jean-Christophe PLAGNIOL-VILLARD
2013-01-25 23:55   ` [PATCH 2/7] globalvar: add it's own bus Jean-Christophe PLAGNIOL-VILLARD
2013-01-25 23:55   ` Jean-Christophe PLAGNIOL-VILLARD [this message]
2013-01-25 23:55   ` [PATCH 4/7] net: switch to global device Jean-Christophe PLAGNIOL-VILLARD
2013-01-25 23:55   ` [PATCH 5/7] dhcp: switch globalvar to it's own device Jean-Christophe PLAGNIOL-VILLARD
2013-01-25 23:55   ` [PATCH 6/7] bootm: " Jean-Christophe PLAGNIOL-VILLARD
2013-01-25 23:55   ` [PATCH 7/7] bootargs: " Jean-Christophe PLAGNIOL-VILLARD
2013-01-26  0:45   ` [PATCH 1/7] params: allow to access first sub-device and params via env Alexander Aring
2013-01-27 10:08 ` [PATCH 0/7] globalvar: add multiple device support Sascha Hauer
2013-01-27 10:38   ` Jean-Christophe PLAGNIOL-VILLARD
2013-01-27 10:56     ` Sascha Hauer
2013-03-13 18:01 [PATCH 0/7 v2] " Jean-Christophe PLAGNIOL-VILLARD
2013-03-13 18:05 ` [PATCH 1/7] params: allow to access first sub-device and params via env Jean-Christophe PLAGNIOL-VILLARD
2013-03-13 18:05   ` [PATCH 3/7] globalvar: allow to register multiple device Jean-Christophe PLAGNIOL-VILLARD

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1359158132-8023-3-git-send-email-plagnioj@jcrosoft.com \
    --to=plagnioj@jcrosoft.com \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox