aboutsummaryrefslogtreecommitdiff
path: root/apps/systemlib/param
diff options
context:
space:
mode:
authorpx4dev <px4@purgatory.org>2012-08-18 22:04:32 -0700
committerpx4dev <px4@purgatory.org>2012-08-19 01:31:26 -0700
commitd91f5f3dd7d80b690956d02807aa74253aebe19c (patch)
tree01ebae3626826e7504ab04caa83448487752ac62 /apps/systemlib/param
parentcd8a085e00a546b36b8b5a5bae125350008cf8a0 (diff)
downloadpx4-firmware-d91f5f3dd7d80b690956d02807aa74253aebe19c.tar.gz
px4-firmware-d91f5f3dd7d80b690956d02807aa74253aebe19c.tar.bz2
px4-firmware-d91f5f3dd7d80b690956d02807aa74253aebe19c.zip
The beginnings of a new parameter system.
Diffstat (limited to 'apps/systemlib/param')
-rw-r--r--apps/systemlib/param/param.c110
-rw-r--r--apps/systemlib/param/param.h140
2 files changed, 250 insertions, 0 deletions
diff --git a/apps/systemlib/param/param.c b/apps/systemlib/param/param.c
new file mode 100644
index 000000000..bb82941a0
--- /dev/null
+++ b/apps/systemlib/param/param.c
@@ -0,0 +1,110 @@
+/**
+ * @file param.c
+ *
+ * Global parameter store.
+ */
+
+#include <string.h>
+#include <stdbool.h>
+
+#include <unistd.h>
+
+#include "param.h"
+
+/**
+ * Array of static parameter info.
+ */
+extern char __param_start, __param_end;
+static const struct param_info_s *param_info_base = (struct param_info_s *)&__param_start;
+static const struct param_info_s *param_info_limit = (struct param_info_s *)&__param_end;
+#define param_info_count ((unsigned)(param_info_limit - param_info_base))
+
+static bool
+handle_in_range(param_t handle)
+{
+ return (handle < param_info_count);
+}
+
+param_t
+param_find(const char *name)
+{
+ param_t param;
+
+ for (param = 0; handle_in_range(param); param++) {
+ if (!strcmp(param_info_base[param].name, name))
+ return param;
+ }
+
+ /* not found */
+ return PARAM_INVALID;
+}
+
+enum
+param_type_e param_type(param_t param)
+{
+ if (handle_in_range(param))
+ return param_info_base[param].type;
+
+ return PARAM_TYPE_UNKNOWN;
+}
+
+size_t
+param_size(param_t param)
+{
+
+ if (handle_in_range(param)) {
+ switch (param_info_base[param].type) {
+ case PARAM_TYPE_INT32:
+ case PARAM_TYPE_FLOAT:
+ return 4;
+
+ case PARAM_TYPE_STRUCT ... PARAM_TYPE_STRUCT_MAX:
+ return param_info_base[param].type - PARAM_TYPE_STRUCT;
+
+ default:
+ return 0;
+ }
+ }
+
+ return 0;
+}
+
+int
+param_get(param_t param, void *val)
+{
+ if (handle_in_range(param)) {
+
+ /* XXX look for updated value stored elsewhere */
+
+ switch (param_info_base[param].type) {
+ case PARAM_TYPE_INT32:
+ *(int32_t *)val = param_info_base[param].i;
+ return 0;
+
+ case PARAM_TYPE_FLOAT:
+ *(float *)val = param_info_base[param].f;
+ return 0;
+
+ case PARAM_TYPE_STRUCT ... PARAM_TYPE_STRUCT_MAX:
+ memcpy(val, param_info_base[param].p, param_size(param));
+ return 0;
+
+ default:
+ return -1;
+ }
+ }
+ return -1;
+}
+
+int
+param_set(param_t param, void *val)
+{
+ if (handle_in_range(param)) {
+
+ /* XXX maintain list of changed values */
+
+ }
+ return -1;
+
+}
+
diff --git a/apps/systemlib/param/param.h b/apps/systemlib/param/param.h
new file mode 100644
index 000000000..9f7808f30
--- /dev/null
+++ b/apps/systemlib/param/param.h
@@ -0,0 +1,140 @@
+/**
+ * @file param.h
+ *
+ * Global parameter store.
+ */
+
+#ifndef _SYSTEMLIB_PARAM_PARAM_H
+#define _SYSTEMLIB_PARAM_PARAM_H
+
+#include <stdint.h>
+
+/**
+ * Parameter types.
+ */
+typedef enum param_type_e
+{
+ /* globally-known parameter types */
+ PARAM_TYPE_INT32 = 0,
+ PARAM_TYPE_FLOAT,
+
+ /* structure parameters; these are expected to be identified by name */
+ PARAM_TYPE_STRUCT = 100,
+ PARAM_TYPE_STRUCT_MAX = 16384 + PARAM_TYPE_STRUCT,
+
+ PARAM_TYPE_UNKNOWN = 0xffff
+} param_type_t;
+
+/**
+ * Parameter handle.
+ *
+ * Parameters are represented by parameter handles, which can
+ * be obtained by looking up (or creating?) parameters.
+ */
+typedef uintptr_t param_t;
+
+/**
+ * Handle returned when a parameter cannot be found.
+ */
+#define PARAM_INVALID ((uintptr_t)0xffffffff)
+
+/**
+ * Look up a parameter by name.
+ *
+ * @param name The canonical name of the parameter being looked up.
+ * @return A handle to the parameter, or PARAM_INVALID if the parameter does not exist.
+ */
+__EXPORT param_t param_find(const char *name);
+
+/**
+ * Obtain the type of a parameter.
+ *
+ * @param param A handle returned by param_find.
+ * @return The type assigned to the parameter.
+ */
+__EXPORT param_type_t param_type(param_t param);
+
+/**
+ * Determine the size of a parameter.
+ *
+ * @param param A handle returned by param_find.
+ * @return The size of the parameter's value.
+ */
+__EXPORT size_t param_size(param_t param);
+
+/**
+ * Obtain the scalar value of a parameter.
+ *
+ * @param param A handle returned by param_find.
+ * @param val Where to return the value, assumed to point to suitable storage for the parameter type.
+ * For structures, a pointer to the structure is returned.
+ * @return Zero if the parameter's value could be returned as a scalar, nonzero otherwise.
+ */
+__EXPORT int param_get(param_t param, void *val);
+
+/**
+ * Set the scalar value of a parameter.
+ *
+ * @param param A handle returned by param_find.
+ * @param val The value to set; assumed to point to a variable of the parameter type.
+ * For structures, the pointer is assumed to point to a copy of the structure.
+ * @return Zero if the parameter's value could be set from a scalar, nonzero otherwise.
+ */
+__EXPORT int param_set(param_t param, void *val);
+
+/*
+ * Macros creating static parameter definitions.
+ *
+ * Note that these structures are not known by name; they are
+ * collected into a section that is iterated by the parameter
+ * code.
+ */
+
+/** define an int32 parameter */
+#define PARAM_DEFINE_INT32(_name, _default) \
+ static const \
+ __attribute__((used, section("__param"))) \
+ struct param_info_s __param__##_name = { \
+ .name = #_name, \
+ .type = PARAM_TYPE_INT32, \
+ .i = _default \
+ }
+
+/** define a float parameter */
+#define PARAM_DEFINE_FLOAT(_name, _default) \
+ static const \
+ __attribute__((used, section("__param"))) \
+ struct param_info_s __param__##_name = { \
+ .name = #_name, \
+ .type = PARAM_TYPE_FLOAT, \
+ .f = _default \
+ }
+
+/** define a parameter that points to a structure */
+#define PARAM_DEFINE_STRUCT(_name, _default) \
+ static const \
+ __attribute__((used, section("__param"))) \
+ struct param_info_s __param__##_name = { \
+ .name = #_name, \
+ .type = PARAM_TYPE_STRUCT + sizeof(_default), \
+ .p = &_default; \
+ }
+
+/**
+ * Static parameter definition structure.
+ *
+ * This is normally not used by user code; see the PARAM_DEFINE macros
+ * instead.
+ */
+struct param_info_s
+{
+ const char *name;
+ param_type_t type;
+ union {
+ void *p;
+ int32_t i;
+ float f;
+ };
+};
+
+#endif