controller-legacy/models/controller.c
2020-02-08 13:50:54 +01:00

181 lines
4.5 KiB
C

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <uuid/uuid.h>
#include <models/controller.h>
#include <macros.h>
controller*
controller_create(void)
{
controller *result = malloc(sizeof(*result));
uuid_generate(result->uuid);
strcpy(result->name, "new emgauwa device");
result->port = 0;
result->relay_count = 10;
result->relays = malloc(sizeof(*result->relays) * result->relay_count);
uint8_t i;
for(i = 0; i < result->relay_count; i++)
{
result->relays[i] = relay_init(i);
}
return result;
}
void
controller_read_single(MDB_txn *mdb_txn, MDB_dbi mdb_dbi, controller_db_key key_meta, MDB_val *value)
{
int err;
MDB_val key;
key.mv_size = sizeof(controller_db_key);
key.mv_data = &key_meta;
if((err = mdb_get(mdb_txn, mdb_dbi, &key, value)) != 0)
{
fprintf(stderr, "mdb_get error %s\n", mdb_strerror(err));
exit(1);
}
}
controller*
controller_read(MDB_env *mdb_env)
{
int err;
MDB_txn *mdb_txn;
MDB_dbi mdb_dbi;
controller *new_controller;
if((err = mdb_txn_begin(mdb_env, NULL, MDB_RDONLY, &mdb_txn)) != 0)
{
fprintf(stderr, "mdb_txn_begin error %s\n", mdb_strerror(err));
exit(1);
}
if((err = mdb_dbi_open(mdb_txn, "meta", 0, &mdb_dbi)) != 0)
{
switch(err)
{
case MDB_NOTFOUND:
mdb_txn_abort(mdb_txn);
new_controller = controller_create();
controller_save(new_controller, mdb_env);
return new_controller;
default:
fprintf(stderr, "mdb_txn_begin error %s\n", mdb_strerror(err));
exit(1);
}
}
new_controller = malloc(sizeof(*new_controller));
MDB_val value;
controller_read_single(mdb_txn, mdb_dbi, KEY_META_UUID, &value);
memmove(new_controller->uuid, (uuid_t*)value.mv_data, sizeof(uuid_t));
controller_read_single(mdb_txn, mdb_dbi, KEY_META_NAME, &value);
strncpy(new_controller->name, (char*)value.mv_data, CONTROLLER_NAME_LENGTH);
new_controller->name[CONTROLLER_NAME_LENGTH] = '\0';
controller_read_single(mdb_txn, mdb_dbi, KEY_META_PORT, &value);
new_controller->port = ((uint16_t*)value.mv_data)[0];
controller_read_single(mdb_txn, mdb_dbi, KEY_META_RELAY_COUNT, &value);
new_controller->relay_count = ((uint8_t*)value.mv_data)[0];
controller_debug(new_controller);
mdb_txn_abort(mdb_txn); // transaction is read only
return new_controller;
}
int
controller_save_single(MDB_txn *mdb_txn, MDB_dbi mdb_dbi, controller_db_key key_meta, MDB_val value)
{
int err;
MDB_val key;
key.mv_size = sizeof(controller_db_key);
key.mv_data = &key_meta;
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;
}
return 0;
}
int
controller_save(controller *cntrlr, MDB_env *mdb_env)
{
int err;
MDB_txn *mdb_txn;
MDB_dbi mdb_dbi;
MDB_val value;
controller_debug(cntrlr);
if((err = mdb_txn_begin(mdb_env, NULL, 0, &mdb_txn)) != 0)
{
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);
}
value.mv_size = sizeof(cntrlr->uuid);
value.mv_data = cntrlr->uuid;
if(controller_save_single(mdb_txn, mdb_dbi, KEY_META_UUID, value))
{
return 1;
}
value.mv_size = sizeof(char) * strlen(cntrlr->name);
value.mv_data = cntrlr->name;
if(controller_save_single(mdb_txn, mdb_dbi, KEY_META_NAME, value))
{
return 1;
}
value.mv_size = sizeof(cntrlr->port);
value.mv_data = &cntrlr->port;
if(controller_save_single(mdb_txn, mdb_dbi, KEY_META_PORT, value))
{
return 1;
}
value.mv_size = sizeof(cntrlr->relay_count);
value.mv_data = &cntrlr->relay_count;
if(controller_save_single(mdb_txn, mdb_dbi, KEY_META_RELAY_COUNT, value))
{
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);
printf("port: %5d relays: %3d\n", cntrlr->port, cntrlr->relay_count);
}