aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-light/src/hicn/config/command.c
diff options
context:
space:
mode:
authorJordan Augé <jordan.auge+fdio@cisco.com>2020-09-23 17:50:52 +0200
committerMauro Sardara <msardara@cisco.com>2021-03-19 14:15:14 +0100
commita070b0de9f9e9cbca150eea4eda74757ca588bed (patch)
tree9f2a11fa1afcd51b0b14f4b26bebf4deb8289a2f /hicn-light/src/hicn/config/command.c
parent32dccec98e4c7d7e4ce902e19ba8d1b29b823758 (diff)
[HICN-645] Control plane (WIP)
Change-Id: I4be6a40b690b62f22f57de6d8c10b01a1be42a6d Signed-off-by: Jordan Augé <jordan.auge+fdio@cisco.com> Signed-off-by: Enrico Loparco (eloparco) <eloparco@cisco.com> Signed-off-by: Mauro Sardara <msardara@cisco.com>
Diffstat (limited to 'hicn-light/src/hicn/config/command.c')
-rw-r--r--hicn-light/src/hicn/config/command.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/hicn-light/src/hicn/config/command.c b/hicn-light/src/hicn/config/command.c
new file mode 100644
index 000000000..4c89d6680
--- /dev/null
+++ b/hicn-light/src/hicn/config/command.c
@@ -0,0 +1,61 @@
+
+/**
+ * @file command.c
+ * @brief Implementation of commands.
+ */
+
+#include <search.h> /* tfind, tdestroy, twalk */
+#include <stdio.h>
+
+#include "command.h"
+
+/* Commands are registered in the following tree. */
+static void * commands_root = NULL; /**< Tree ordered by name */
+
+static void nothing_to_free() {}
+
+#ifdef __linux__
+__attribute__((destructor))
+static
+void
+command_clear()
+{
+ tdestroy(commands_root, nothing_to_free);
+}
+#endif /* __linux__ */
+
+static
+int
+_command_compare(const command_parser_t * c1,
+ const command_parser_t * c2)
+{
+ if (c1->action != c2->action)
+ return c2->action - c1->action;
+ if (c1->object != c2->object)
+ return c2->object - c1->object;
+ if (c1->nparams != c2->nparams)
+ return c2->nparams - c1->nparams;
+ return 0;
+}
+
+#define command_compare (int (*)(const void *, const void *))(_command_compare)
+
+void
+command_register(const command_parser_t * command)
+{
+ // Insert the command in the tree if the keys does not exist yet
+ tsearch(command, &commands_root, command_compare);
+}
+
+const command_parser_t *
+command_search(hc_action_t action, hc_object_type_t object, unsigned nparams)
+{
+ command_parser_t ** command, search;
+
+ search.action = action;
+ search.object = object;
+ search.nparams = nparams;
+ command = tfind(&search, &commands_root, command_compare);
+
+ return command ? *command : NULL;
+}