add: save/load for uuid and name

This commit is contained in:
Tobias Reisinger 2020-01-07 02:23:16 +01:00
parent 270c87e0bc
commit db7a377250
11 changed files with 208 additions and 82 deletions

View file

@ -5,15 +5,14 @@ add_executable(controller main.c)
option(WIRING_PI_DEBUG "Use WiringPi Debugging Tool" OFF)
SET(CMAKE_C_FLAGS "-Wall -Wextra -lwiringPi -luuid -g")
SET(CMAKE_C_FLAGS "-Wall -Wextra -lwiringPi -luuid -llmdb -g")
if(WIRING_PI_DEBUG)
add_compile_definitions("WIRING_PI_DEBUG")
endif(WIRING_PI_DEBUG)
aux_source_directory(. SRC_DIR)
aux_source_directory(models MODEL_SRC)
target_sources(controller PRIVATE ${SRC_DIR} ${MODEL_SRC})
target_include_directories(controller PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_include_directories(controller PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)

15
include/config.h Normal file
View file

@ -0,0 +1,15 @@
#ifndef CONTROLLER_CONFIG_H
#define CONTROLLER_CONFIG_H
#define CONTROLLER_FILE_NAME "emgauwa-controller.dat"
#define CONTROLLER_FILE_VERSION 1
#define CONTROLLER_FILE_HEADER_SIZE 4
#define CONTROLLER_NAME_LENGTH 128
#define MDB_MAXDBS 8
#define TEST_KEY "blubb"
#define TEST_VALUE "bla bla bla"
#endif //CONTROLLER_CONFIG_H

4
include/display.h Normal file
View file

@ -0,0 +1,4 @@
#include <stdint.h>
void
display_setup(void);

6
include/macros.h Normal file
View file

@ -0,0 +1,6 @@
#ifndef CONTROLLER_MACROS_H
#define CONTROLLER_MACROS_H
#define LOG_ERROR(msg) printf("ERROR: \"%s\" in function %s in file %s:%d\n", msg, __func__, __FILE__, __LINE__)
#endif //CONTROLLER_MACROS_H

View file

@ -0,0 +1,42 @@
#ifndef CONTROLLER_CONTROLLER_H
#define CONTROLLER_CONTROLLER_H
#include <uuid/uuid.h>
#include <stdint.h>
#include <lmdb.h>
#include <config.h>
#include <models/relay.h>
typedef struct controller
{
uuid_t uuid;
char name[CONTROLLER_NAME_LENGTH + 1];
uint16_t port;
uint8_t relay_count;
relay **relays;
} controller;
typedef enum controller_db_key
{
KEY_META_UUID,
KEY_META_NAME,
KEY_META_PORT,
KEY_META_RELAY_COUNT,
KEY_META_RELAYS
} controller_db_key;
controller*
controller_create(void);
controller*
controller_read(MDB_env *mdb_env);
int
controller_save(controller *cntrlr, MDB_env *mdb_env);
void
controller_debug(controller *cntrlr);
#endif //CONTROLLER_CONTROLLER_H

7
include/wiring_debug.h Normal file
View file

@ -0,0 +1,7 @@
#ifdef WIRING_PI_DEBUG
#define wiringPiSetup() printf("Setting up wiringPi....\n")
#define pinMode(x,y) printf("pinMode(%d, %d)\n",x,y)
#define digitalWrite(x,y) printf("digitalWrite(%d, %d)\n",x,y)
#endif

34
main.c
View file

@ -1,3 +1,8 @@
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <lmdb.h>
#include <models/controller.h>
int
@ -5,8 +10,33 @@ main(int argc, char** argv)
{
(void)argc;
(void)argv;
controller *this = controller_read();
controller_save(this);
int err;
MDB_env *mdb_env;
if(mdb_env_create(&mdb_env) != 0)
{
perror("Can't create mdb handle");
exit(1);
}
if((err = mdb_env_set_maxdbs(mdb_env, MDB_MAXDBS)) != 0)
{
fprintf(stderr, "mdb_env_set_maxdbs error %s\n", mdb_strerror(err));
}
if(mdb_env_open(mdb_env, "db.lmdb", MDB_NOSUBDIR, 0700) != 0)
{
perror("Can't open mdb file");
exit(1);
}
controller *this = controller_read(mdb_env);
(void)this;
//controller_save(this);
mdb_env_close(mdb_env);
return 0;
}

View file

@ -5,10 +5,8 @@
#include <uuid/uuid.h>
#include <models/controller.h>
#include <models/controller_file_reader.h>
#include <macros.h>
controller*
controller_create(void)
{
@ -30,76 +28,129 @@ controller_create(void)
}
controller*
controller_read(void)
controller_read(MDB_env *mdb_env)
{
FILE *fp;
controller *result;
int err;
int meta_key_data;
MDB_txn *mdb_txn;
MDB_dbi mdb_dbi;
fp = fopen(CONTROLLER_FILE_NAME, "rb");
controller *new_controller;
if(fp == NULL)
if((err = mdb_txn_begin(mdb_env, NULL, MDB_RDONLY, &mdb_txn)) != 0)
{
LOG_ERROR("Error opening file");
result = controller_create();
return result;
fprintf(stderr, "mdb_txn_begin error %s\n", mdb_strerror(err));
exit(1);
}
uint8_t header[CONTROLLER_FILE_HEADER_SIZE];
size_t read_size;
read_size = fread(header, sizeof(*header), CONTROLLER_FILE_HEADER_SIZE, fp);
if(ferror(fp) || read_size != CONTROLLER_FILE_HEADER_SIZE)
if((err = mdb_dbi_open(mdb_txn, "meta", 0, &mdb_dbi)) != 0)
{
fclose(fp);
LOG_ERROR("Error reading header");
result = controller_create();
return result;
}
uint8_t version = header[0];
printf("%d\n", version);
switch(version)
switch(err)
{
case 1:
controller_file_reader_1(fp);
break;
case MDB_NOTFOUND:
mdb_txn_abort(mdb_txn);
new_controller = controller_create();
controller_save(new_controller, mdb_env);
return new_controller;
default:
fclose(fp);
LOG_ERROR("Error unsupported version number");
fprintf(stderr, "mdb_txn_begin error %s\n", mdb_strerror(err));
exit(1);
}
}
new_controller = malloc(sizeof(*new_controller));
MDB_val key;
MDB_val value;
key.mv_size = sizeof(controller_db_key);
meta_key_data = KEY_META_UUID;
key.mv_data = &meta_key_data;
if((err = mdb_get(mdb_txn, mdb_dbi, &key, &value)) != 0)
{
fprintf(stderr, "mdb_get error %s\n", mdb_strerror(err));
}
memmove(new_controller->uuid, (uuid_t*)value.mv_data, sizeof(uuid_t));
meta_key_data = KEY_META_NAME;
key.mv_data = &meta_key_data;
if((err = mdb_get(mdb_txn, mdb_dbi, &key, &value)) != 0)
{
fprintf(stderr, "mdb_get error %s\n", mdb_strerror(err));
}
strncpy(new_controller->name, (char*)value.mv_data, CONTROLLER_NAME_LENGTH);
new_controller->name[CONTROLLER_NAME_LENGTH] = '\0';
controller_debug(new_controller);
mdb_txn_abort(mdb_txn); // transaction is read only
return NULL;
}
fclose(fp);
result = controller_create();
return result;
}
int
controller_save(controller* ci)
controller_save(controller *cntrlr, MDB_env *mdb_env)
{
FILE *fp;
int err;
int meta_key_data;
fp = fopen(CONTROLLER_FILE_NAME, "w+b");
MDB_txn *mdb_txn;
MDB_dbi mdb_dbi;
MDB_val key;
MDB_val value;
if(fp == NULL)
controller_debug(cntrlr);
if((err = mdb_txn_begin(mdb_env, NULL, 0, &mdb_txn)) != 0)
{
LOG_ERROR("Error creating or opening file");
fprintf(stderr, "mdb_txn_begin error %s\n", mdb_strerror(err));
exit(1);
}
if((err = mdb_dbi_open(mdb_txn, "meta", MDB_CREATE, &mdb_dbi)) != 0)
{
fprintf(stderr, "mdb_dbi_open error %s\n", mdb_strerror(err));
exit(1);
}
key.mv_size = sizeof(controller_db_key);
meta_key_data = KEY_META_UUID;
key.mv_data = &meta_key_data;
value.mv_size = sizeof(cntrlr->uuid);
value.mv_data = cntrlr->uuid;
if((err = mdb_put(mdb_txn, mdb_dbi, &key, &value, 0)) != 0)
{
fprintf(stderr, "mdb_put error %s\n", mdb_strerror(err));
mdb_txn_abort(mdb_txn);
return 1;
}
uint8_t header[CONTROLLER_FILE_HEADER_SIZE];
header[0] = CONTROLLER_FILE_VERSION;
meta_key_data = KEY_META_NAME;
key.mv_data = &meta_key_data;
fwrite(header, sizeof(*header), CONTROLLER_FILE_HEADER_SIZE, fp);
fwrite(ci->name, sizeof(*ci->name), CONTROLLER_NAME_LENGTH, fp);
value.mv_size = sizeof(char) * strlen(cntrlr->name);
value.mv_data = cntrlr->name;
fclose(fp);
if((err = mdb_put(mdb_txn, mdb_dbi, &key, &value, 0)) != 0)
{
fprintf(stderr, "mdb_put error %s\n", mdb_strerror(err));
mdb_txn_abort(mdb_txn);
return 1;
}
mdb_txn_commit(mdb_txn);
return 0;
}
void
controller_debug(controller *cntrlr)
{
char uuid_str[37];
uuid_unparse(cntrlr->uuid, uuid_str);
printf("%s @ %p\n", uuid_str, cntrlr);
printf("name: %s\n", cntrlr->name);
}

View file

@ -1,28 +0,0 @@
#ifndef CONTROLLER_CONTROLLER_H
#define CONTROLLER_CONTROLLER_H
#include <uuid/uuid.h>
#include <stdint.h>
#include <config.h>
#include <models/relay.h>
typedef struct controller {
uuid_t uuid;
char name[CONTROLLER_NAME_LENGTH];
uint16_t port;
uint8_t relay_count;
relay **relays;
} controller;
controller*
controller_create(void);
controller*
controller_read(void);
int
controller_save(controller* ci);
#endif //CONTROLLER_CONTROLLER_H

View file

@ -1,6 +1,6 @@
#include <stdlib.h>
#include "relay.h"
#include <models/relay.h>
relay*
relay_init(uint8_t index)