2020-02-08 14:09:34 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <uuid/uuid.h>
|
|
|
|
|
|
|
|
#include <models/controller.h>
|
|
|
|
#include <macros.h>
|
|
|
|
|
|
|
|
static void
|
2020-04-13 22:50:55 +00:00
|
|
|
controller_load_single(MDB_txn *mdb_txn, MDB_dbi mdb_dbi, db_key_controller_e key_controller, MDB_val *value)
|
2020-02-08 14:09:34 +00:00
|
|
|
{
|
|
|
|
int err;
|
|
|
|
MDB_val key;
|
|
|
|
|
2020-04-13 22:50:55 +00:00
|
|
|
key.mv_size = sizeof(db_key_controller_e);
|
|
|
|
key.mv_data = &key_controller;
|
2020-02-08 14:09:34 +00:00
|
|
|
|
|
|
|
if((err = mdb_get(mdb_txn, mdb_dbi, &key, value)) != 0)
|
|
|
|
{
|
2020-04-24 13:08:26 +00:00
|
|
|
LOG_ERROR("mdb_get error %s\n", mdb_strerror(err));
|
2020-02-08 14:09:34 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:50:55 +00:00
|
|
|
controller_t*
|
2020-02-08 14:09:34 +00:00
|
|
|
controller_load(MDB_env *mdb_env)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
MDB_txn *mdb_txn;
|
|
|
|
MDB_dbi mdb_dbi;
|
|
|
|
|
2020-04-13 22:50:55 +00:00
|
|
|
controller_t *new_controller;
|
2020-02-08 14:09:34 +00:00
|
|
|
|
|
|
|
if((err = mdb_txn_begin(mdb_env, NULL, MDB_RDONLY, &mdb_txn)) != 0)
|
|
|
|
{
|
2020-04-24 13:08:26 +00:00
|
|
|
LOG_ERROR("mdb_txn_begin error %s\n", mdb_strerror(err));
|
2020-02-09 23:58:17 +00:00
|
|
|
return NULL;
|
2020-02-08 14:09:34 +00:00
|
|
|
}
|
2020-04-13 22:50:55 +00:00
|
|
|
if((err = mdb_dbi_open(mdb_txn, "controller", 0, &mdb_dbi)) != 0)
|
2020-02-08 14:09:34 +00:00
|
|
|
{
|
|
|
|
switch(err)
|
|
|
|
{
|
|
|
|
case MDB_NOTFOUND:
|
2020-04-24 13:08:26 +00:00
|
|
|
LOG_INFO("no controller found in db. creating new one\n");
|
2020-02-08 14:09:34 +00:00
|
|
|
mdb_txn_abort(mdb_txn);
|
2020-04-13 22:50:55 +00:00
|
|
|
new_controller = controller_create();
|
2020-02-08 14:09:34 +00:00
|
|
|
controller_save(new_controller, mdb_env);
|
|
|
|
return new_controller;
|
|
|
|
default:
|
2020-04-24 13:08:26 +00:00
|
|
|
LOG_ERROR("mdb_txn_begin error %s\n", mdb_strerror(err));
|
2020-02-09 23:58:17 +00:00
|
|
|
return NULL;
|
2020-02-08 14:09:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-13 22:50:55 +00:00
|
|
|
new_controller = malloc(sizeof(controller_t));
|
2020-02-08 14:09:34 +00:00
|
|
|
|
|
|
|
MDB_val value;
|
|
|
|
|
2020-04-13 22:50:55 +00:00
|
|
|
controller_load_single(mdb_txn, mdb_dbi, DB_KEY_CONTROLLER_ID, &value);
|
2020-02-29 09:23:44 +00:00
|
|
|
memmove(new_controller->id, (uuid_t*)value.mv_data, sizeof(uuid_t));
|
2020-02-08 14:09:34 +00:00
|
|
|
|
2020-04-13 22:50:55 +00:00
|
|
|
controller_load_single(mdb_txn, mdb_dbi, DB_KEY_CONTROLLER_NAME, &value);
|
|
|
|
strncpy(new_controller->name, (char*)value.mv_data, MAX_NAME_LENGTH);
|
|
|
|
new_controller->name[MAX_NAME_LENGTH] = '\0';
|
2020-02-08 14:09:34 +00:00
|
|
|
|
2020-04-13 22:50:55 +00:00
|
|
|
controller_load_single(mdb_txn, mdb_dbi, DB_KEY_CONTROLLER_COMMAND_PORT, &value);
|
2020-02-09 23:58:17 +00:00
|
|
|
new_controller->command_port = ((uint16_t*)value.mv_data)[0];
|
|
|
|
|
2020-04-13 22:50:55 +00:00
|
|
|
controller_load_single(mdb_txn, mdb_dbi, DB_KEY_CONTROLLER_DISCOVERY_PORT, &value);
|
2020-02-09 23:58:17 +00:00
|
|
|
new_controller->discovery_port = ((uint16_t*)value.mv_data)[0];
|
2020-02-08 14:09:34 +00:00
|
|
|
|
2020-04-16 23:38:25 +00:00
|
|
|
new_controller->relay_count = global_config.relay_count;
|
2020-02-08 14:09:34 +00:00
|
|
|
|
|
|
|
mdb_txn_abort(mdb_txn); // transaction is read only
|
2020-04-13 22:50:55 +00:00
|
|
|
|
|
|
|
new_controller->relays = malloc(sizeof(relay_t*) * new_controller->relay_count);
|
|
|
|
for(uint8_t i = 0; i < new_controller->relay_count; i++)
|
|
|
|
{
|
2020-04-24 13:08:26 +00:00
|
|
|
LOG_TRACE("loading relay %d\n", i);
|
2020-04-13 22:50:55 +00:00
|
|
|
new_controller->relays[i] = relay_load(mdb_env, i);
|
|
|
|
}
|
2020-02-08 14:09:34 +00:00
|
|
|
|
|
|
|
return new_controller;
|
|
|
|
}
|