156 lines
3.7 KiB
C
156 lines
3.7 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;
|
|
}
|
|
|
|
controller*
|
|
controller_read(MDB_env *mdb_env)
|
|
{
|
|
int err;
|
|
int meta_key_data;
|
|
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 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;
|
|
}
|
|
|
|
int
|
|
controller_save(controller *cntrlr, MDB_env *mdb_env)
|
|
{
|
|
int err;
|
|
int meta_key_data;
|
|
|
|
MDB_txn *mdb_txn;
|
|
MDB_dbi mdb_dbi;
|
|
MDB_val key;
|
|
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);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
meta_key_data = KEY_META_NAME;
|
|
key.mv_data = &meta_key_data;
|
|
|
|
value.mv_size = sizeof(char) * strlen(cntrlr->name);
|
|
value.mv_data = cntrlr->name;
|
|
|
|
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);
|
|
}
|