From e9162b67d1ce9de3948fe3d8bbf05b853da24c34 Mon Sep 17 00:00:00 2001 From: XANTRONIX Development Date: Tue, 21 May 2019 09:25:11 -0500 Subject: [PATCH] Initial implementation of hexagram_sim --- include/hexagram/sim.h | 23 ++++++++++++ src/Makefile | 4 +-- src/sim.c | 79 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 include/hexagram/sim.h create mode 100644 src/sim.c diff --git a/include/hexagram/sim.h b/include/hexagram/sim.h new file mode 100644 index 0000000..f270af4 --- /dev/null +++ b/include/hexagram/sim.h @@ -0,0 +1,23 @@ +#ifndef _HEXAGRAM_SIM_H +#define _HEXAGRAM_SIM_H + +#include +#include +#include + +typedef struct _hexagram_sim hexagram_sim; + +hexagram_sim *hexagram_sim_new(); + +void hexagram_sim_destroy(hexagram_sim *sim); + +int hexagram_sim_add_can_if(hexagram_sim *sim, + const char *name, + hexagram_can_if *can_if); + +int hexagram_sim_add_module(hexagram_sim *sim, + hexagram_module *module); + +int hexagram_sim_run(hexagram_sim *sim); + +#endif /* _HEXAGRAM_SIM_H */ diff --git a/src/Makefile b/src/Makefile index 5969c28..e2194d9 100644 --- a/src/Makefile +++ b/src/Makefile @@ -7,10 +7,10 @@ CC = $(CROSS)cc CFLAGS += -fPIC -I$(INCLUDE_PATH) LDFLAGS = -HEADERS = dict.h hash.h can.h capture.h pcapng.h module.h +HEADERS = dict.h hash.h can.h capture.h pcapng.h module.h sim.h HEADERS_LOCAL = util.h -OBJS = dict.o hash.o can.o capture.o pcapng.o module.o +OBJS = dict.o hash.o can.o capture.o pcapng.o module.o sim.o VERSION_MAJOR = 0 VERSION_MINOR = 0.1 diff --git a/src/sim.c b/src/sim.c new file mode 100644 index 0000000..a96caae --- /dev/null +++ b/src/sim.c @@ -0,0 +1,79 @@ +#include +#include + +#include + +struct _hexagram_sim { + hexagram_dict *can_ifs; + hexagram_dict *modules; +}; + +hexagram_sim *hexagram_sim_new() { + hexagram_sim *sim; + + if ((sim = malloc(sizeof(*sim))) == NULL) { + goto error_malloc; + } + + if ((sim->can_ifs = hexagram_dict_new()) == NULL) { + goto error_dict_new_can_ifs; + } + + if ((sim->modules = hexagram_dict_new()) == NULL) { + goto error_dict_new_modules; + } + + return sim; + +error_dict_new_modules: + hexagram_dict_destroy(sim->can_ifs); + +error_dict_new_can_ifs: + free(sim); + +error_malloc: + return NULL; +} + +void hexagram_sim_destroy(hexagram_sim *sim) { + hexagram_dict_destroy(sim->modules); + hexagram_dict_destroy(sim->can_ifs); + + free(sim); +} + +int hexagram_sim_add_can_if(hexagram_sim *sim, + const char *name, + hexagram_can_if *can_if) { + if (hexagram_dict_set(sim->can_ifs, + (void *)name, strlen(name), can_if) == NULL) { + goto error_dict_set; + } + + return 0; + +error_dict_set: + return -1; +} + +int hexagram_sim_add_module(hexagram_sim *sim, + hexagram_module *module) { + const char *name; + + if ((name = hexagram_module_name(module)) == NULL) { + goto error_module_name; + } + + if (hexagram_dict_set(sim->modules, + (void *)name, strlen(name), module) == NULL) { + goto error_dict_set; + } + + return 0; + +error_dict_set: +error_module_name: + return -1; +} + +int hexagram_sim_run(hexagram_sim *sim);