From 9fbad1544764c515cb1f22bc552f33bb41206e92 Mon Sep 17 00:00:00 2001 From: cbdev Date: Wed, 14 Jun 2023 22:51:13 +0200 Subject: Skeleton structure implementation --- Makefile | 20 ++++++++++++++++++++ commander.cfg | 12 ++++++++++++ config.c | 29 +++++++++++++++++++++++++++++ config.h | 3 +++ control.c | 13 +++++++++++++ control.h | 2 ++ nfcommander.c | 37 +++++++++++++++++++++++++++++++++++++ nfcommander.h | 8 ++++++++ reader.c | 36 ++++++++++++++++++++++++++++++++++++ reader.h | 2 ++ reader_yhy.c | 11 +++++++++++ reader_yhy.h | 0 12 files changed, 173 insertions(+) create mode 100644 Makefile create mode 100644 commander.cfg create mode 100644 config.c create mode 100644 config.h create mode 100644 control.c create mode 100644 control.h create mode 100644 nfcommander.c create mode 100644 nfcommander.h create mode 100644 reader.c create mode 100644 reader.h create mode 100644 reader_yhy.c create mode 100644 reader_yhy.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9cb714b --- /dev/null +++ b/Makefile @@ -0,0 +1,20 @@ +.PHONY: all + +CFLAGS += -g -Wall -Wpedantic +CORE_OBJS = control.o reader.o config.o +PLUGINS = reader_yhy.so + +%.so: CFLAGS += -shared -fPIC + +%.so :: %.c %.h + $(CC) $(CFLAGS) $< -o $@ $(LDFLAGS) $(LDLIBS) + +nfcommander: LDLIBS = -ldl + +all: nfcommander $(PLUGINS) + +nfcommander: nfcommander.c nfcommander.h $(CORE_OBJS) + +clean: + $(RM) nfcommander + $(RM) $(CORE_OBJS) diff --git a/commander.cfg b/commander.cfg new file mode 100644 index 0000000..ec9895f --- /dev/null +++ b/commander.cfg @@ -0,0 +1,12 @@ +[nfc] +reader=yhy +device=/dev/ttyACM0 +mifare.defaultkeys=FFFFFFFFFFFF,000000000000 +mifare.prodkey=123456123456 + +[control] +bind = 0.0.0.0 7812 + +[command play] +chdir = commands +exec = ./play diff --git a/config.c b/config.c new file mode 100644 index 0000000..f11bbb6 --- /dev/null +++ b/config.c @@ -0,0 +1,29 @@ +#include +#include + +#include "nfcommander.h" +#include "config.h" + +int config_read(char* path){ + //TODO + printf("Reading config file %s\n", path); + + return 0; +} + +char* config_get(char* section, char* property){ + //TODO + printf("Querying for config property %s->%s\n", section, property); + + //TEMP + if(!strcmp(property, "reader")){ + return "yhy"; + } + + return NULL; +} + +void config_free(){ + //TODO + printf("Cleaning up config data\n"); +} diff --git a/config.h b/config.h new file mode 100644 index 0000000..d39b6ff --- /dev/null +++ b/config.h @@ -0,0 +1,3 @@ +int config_read(char* path); +char* config_get(char* section, char* property); +void config_free(); diff --git a/control.c b/control.c new file mode 100644 index 0000000..2965d93 --- /dev/null +++ b/control.c @@ -0,0 +1,13 @@ +#include + +#include "control.h" + +int control_start(){ + //TODO + printf("Starting up control interface\n"); + return 0; +} + +void control_free(){ + printf("Shutting down control interface\n"); +} diff --git a/control.h b/control.h new file mode 100644 index 0000000..9537682 --- /dev/null +++ b/control.h @@ -0,0 +1,2 @@ +int control_start(); +void control_free(); diff --git a/nfcommander.c b/nfcommander.c new file mode 100644 index 0000000..de50ef4 --- /dev/null +++ b/nfcommander.c @@ -0,0 +1,37 @@ +#include + +#include "nfcommander.h" +#include "config.h" +#include "reader.h" +#include "control.h" + +int usage(char* fn){ + printf("NFCommander - trigger actions based on near-field tags\n"); + printf("\tUsage: %s \n", fn); + return EXIT_FAILURE; +} + +int main(int argc, char** argv){ + if(argc < 2){ + return usage(argv[0]); + } + + //read configuration + if(config_read(argv[1])){ + return usage(argv[0]); + } + + //start reader api + reader_init(); + + //start control api + control_start(); + + //handle events + //TODO + + //clean up + reader_free(); + control_free(); + config_free(); +} diff --git a/nfcommander.h b/nfcommander.h new file mode 100644 index 0000000..dbab99f --- /dev/null +++ b/nfcommander.h @@ -0,0 +1,8 @@ +#include +#include +#include + +#ifndef NFCOMMANDER_VERSION + #define NFCOMMANDER_VERSION "v0.1-dev" +#endif + diff --git a/reader.c b/reader.c new file mode 100644 index 0000000..1186800 --- /dev/null +++ b/reader.c @@ -0,0 +1,36 @@ +#include +#include +#include + +#include "reader.h" +#include "config.h" + +#define MAX_PLUGIN_PATH NAME_MAX +void* reader_module = NULL; + +int reader_init(){ + char plugin[MAX_PLUGIN_PATH] = ""; + char* reader = config_get("nfc", "reader"); + + if(!reader){ + printf("No reader plugin configured\n"); + return -1; + } + + snprintf(plugin, sizeof(plugin), "./reader_%s.so", reader); + reader_module = dlopen(plugin, RTLD_NOW); + + if(!reader_module){ + printf("Failed to load reader plugin %s\n", plugin); + return -1; + } + + return 0; +} + +void reader_free(){ + if(reader_module){ + dlclose(reader_module); + reader_module = NULL; + } +} diff --git a/reader.h b/reader.h new file mode 100644 index 0000000..650119a --- /dev/null +++ b/reader.h @@ -0,0 +1,2 @@ +int reader_init(); +void reader_free(); diff --git a/reader_yhy.c b/reader_yhy.c new file mode 100644 index 0000000..da5be5e --- /dev/null +++ b/reader_yhy.c @@ -0,0 +1,11 @@ +#include + +#include "nfcommander.h" + +static void __attribute__((constructor)) init() { + printf("This is the yhy reader plugin\n"); +} + +static void __attribute__((destructor)) cleanup() { + printf("This is the yhy reader plugin cleanup\n"); +} diff --git a/reader_yhy.h b/reader_yhy.h new file mode 100644 index 0000000..e69de29 -- cgit v1.2.3