Initial implementation of hexagram_sim

This commit is contained in:
XANTRONIX Development 2019-05-21 09:25:11 -05:00
parent f9721c6b0e
commit e9162b67d1
3 changed files with 104 additions and 2 deletions

23
include/hexagram/sim.h Normal file
View file

@ -0,0 +1,23 @@
#ifndef _HEXAGRAM_SIM_H
#define _HEXAGRAM_SIM_H
#include <hexagram/dict.h>
#include <hexagram/can.h>
#include <hexagram/module.h>
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 */

View file

@ -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

79
src/sim.c Normal file
View file

@ -0,0 +1,79 @@
#include <stdlib.h>
#include <string.h>
#include <hexagram/sim.h>
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);