summaryrefslogtreecommitdiffstats
path: root/lib/parameter.c
diff options
context:
space:
mode:
authorSascha Hauer <s.hauer@pengutronix.de>2007-07-05 18:02:12 +0200
committerSascha Hauer <sha@octopus.labnet.pengutronix.de>2007-07-05 18:02:12 +0200
commit4a660c08776809ee08b1d0d62fbaf023080d03ae (patch)
treefe960131e7fbd051ca3d258ae3b7000ab3f90a3e /lib/parameter.c
parent24e7509199bc627546fb213158ca04c891c04bc1 (diff)
downloadbarebox-4a660c08776809ee08b1d0d62fbaf023080d03ae.tar.gz
barebox-4a660c08776809ee08b1d0d62fbaf023080d03ae.tar.xz
svn_rev_639
Diffstat (limited to 'lib/parameter.c')
-rw-r--r--lib/parameter.c95
1 files changed, 95 insertions, 0 deletions
diff --git a/lib/parameter.c b/lib/parameter.c
new file mode 100644
index 0000000000..a2490d4ac2
--- /dev/null
+++ b/lib/parameter.c
@@ -0,0 +1,95 @@
+#include <common.h>
+#include <param.h>
+#include <errno.h>
+#include <net.h>
+#include <malloc.h>
+#include <driver.h>
+
+struct param_d *get_param_by_name(struct device_d *dev, const char *name)
+{
+ struct param_d *param = dev->param;
+
+ while (param) {
+ if (!strcmp(param->name, name))
+ return param;
+ param = param->next;
+ }
+
+ return NULL;
+}
+
+const char *dev_get_param(struct device_d *dev, const char *name)
+{
+ struct param_d *param = get_param_by_name(dev, name);
+
+ if (!param) {
+ errno = -EINVAL;
+ return NULL;
+ }
+
+ if (param->get)
+ return param->get(dev, param);
+
+ return param->value;
+}
+
+IPaddr_t dev_get_param_ip(struct device_d *dev, char *name)
+{
+ return string_to_ip(dev_get_param(dev, name));
+}
+
+int dev_set_param_ip(struct device_d *dev, char *name, IPaddr_t ip)
+{
+ char ipstr[16];
+ ip_to_string(ip, ipstr);
+
+ return dev_set_param(dev, name, ipstr);
+}
+
+int dev_set_param(struct device_d *dev, const char *name, const char *val)
+{
+ struct param_d *param;
+
+ if (!dev) {
+ errno = -ENODEV;
+ return -ENODEV;
+ }
+
+ param = get_param_by_name(dev, name);
+
+ if (!param) {
+ errno = -EINVAL;
+ return -EINVAL;
+ }
+
+ if (param->flags & PARAM_FLAG_RO) {
+ errno = -EACCES;
+ return -EACCES;
+ }
+
+ if (param->set)
+ return param->set(dev, param, val);
+
+ if (param->value)
+ free(param->value);
+
+ param->value = strdup(val);
+ return 0;
+}
+
+int dev_add_param(struct device_d *dev, struct param_d *newparam)
+{
+ struct param_d *param = dev->param;
+
+ newparam->next = 0;
+
+ if (param) {
+ while (param->next)
+ param = param->next;
+ param->next = newparam;
+ } else {
+ dev->param = newparam;
+ }
+
+ return 0;
+}