2019-11-15 00:23:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <uuid/uuid.h>
|
|
|
|
|
2019-12-08 22:39:10 +00:00
|
|
|
#include <models/controller.h>
|
2019-11-15 00:23:43 +00:00
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
2020-02-08 12:50:54 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 00:23:43 +00:00
|
|
|
controller*
|
2020-01-07 01:23:16 +00:00
|
|
|
controller_read(MDB_env *mdb_env)
|
2019-11-15 00:23:43 +00:00
|
|
|
{
|
2020-01-07 01:23:16 +00:00
|
|
|
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)
|
2019-11-15 00:23:43 +00:00
|
|
|
{
|
2020-01-07 01:23:16 +00:00
|
|
|
fprintf(stderr, "mdb_txn_begin error %s\n", mdb_strerror(err));
|
|
|
|
exit(1);
|
2019-11-15 00:23:43 +00:00
|
|
|
}
|
2020-01-07 01:23:16 +00:00
|
|
|
if((err = mdb_dbi_open(mdb_txn, "meta", 0, &mdb_dbi)) != 0)
|
2019-11-15 00:23:43 +00:00
|
|
|
{
|
2020-01-07 01:23:16 +00:00
|
|
|
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);
|
|
|
|
}
|
2019-11-15 00:23:43 +00:00
|
|
|
}
|
|
|
|
|
2020-01-07 01:23:16 +00:00
|
|
|
new_controller = malloc(sizeof(*new_controller));
|
|
|
|
|
|
|
|
MDB_val value;
|
2019-11-15 00:23:43 +00:00
|
|
|
|
2020-02-08 12:50:54 +00:00
|
|
|
controller_read_single(mdb_txn, mdb_dbi, KEY_META_UUID, &value);
|
2020-01-07 01:23:16 +00:00
|
|
|
memmove(new_controller->uuid, (uuid_t*)value.mv_data, sizeof(uuid_t));
|
2019-11-15 00:23:43 +00:00
|
|
|
|
2020-02-08 12:50:54 +00:00
|
|
|
controller_read_single(mdb_txn, mdb_dbi, KEY_META_NAME, &value);
|
2020-01-07 01:23:16 +00:00
|
|
|
strncpy(new_controller->name, (char*)value.mv_data, CONTROLLER_NAME_LENGTH);
|
|
|
|
new_controller->name[CONTROLLER_NAME_LENGTH] = '\0';
|
2019-11-15 00:23:43 +00:00
|
|
|
|
2020-02-08 12:50:54 +00:00
|
|
|
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];
|
|
|
|
|
2020-01-07 01:23:16 +00:00
|
|
|
controller_debug(new_controller);
|
2019-11-15 00:23:43 +00:00
|
|
|
|
2020-01-07 01:23:16 +00:00
|
|
|
mdb_txn_abort(mdb_txn); // transaction is read only
|
|
|
|
|
2020-02-08 12:50:54 +00:00
|
|
|
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;
|
2019-11-15 00:23:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-01-07 01:23:16 +00:00
|
|
|
controller_save(controller *cntrlr, MDB_env *mdb_env)
|
2019-11-15 00:23:43 +00:00
|
|
|
{
|
2020-01-07 01:23:16 +00:00
|
|
|
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;
|
2020-02-08 12:50:54 +00:00
|
|
|
if(controller_save_single(mdb_txn, mdb_dbi, KEY_META_UUID, value))
|
2019-11-15 00:23:43 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-01-07 01:23:16 +00:00
|
|
|
value.mv_size = sizeof(char) * strlen(cntrlr->name);
|
|
|
|
value.mv_data = cntrlr->name;
|
2020-02-08 12:50:54 +00:00
|
|
|
if(controller_save_single(mdb_txn, mdb_dbi, KEY_META_NAME, value))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2019-11-15 00:23:43 +00:00
|
|
|
|
2020-02-08 12:50:54 +00:00
|
|
|
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))
|
2020-01-07 01:23:16 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
mdb_txn_commit(mdb_txn);
|
2019-11-15 00:23:43 +00:00
|
|
|
|
|
|
|
return 0;
|
2020-01-07 01:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2020-02-08 12:50:54 +00:00
|
|
|
printf("port: %5d relays: %3d\n", cntrlr->port, cntrlr->relay_count);
|
2020-01-07 01:23:16 +00:00
|
|
|
}
|