controller-legacy/models/controller_save.c

99 lines
2.5 KiB
C
Raw Normal View History

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>
int
2020-04-13 22:50:55 +00:00
controller_save_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_put(mdb_txn, mdb_dbi, &key, &value, 0)) != 0)
{
2020-04-13 22:50:55 +00:00
LOG_ERROR("mdb_put error %s", mdb_strerror(err));
2020-02-08 14:09:34 +00:00
mdb_txn_abort(mdb_txn);
return 1;
}
return 0;
}
int
2020-04-13 22:50:55 +00:00
controller_save(controller_t *controller, MDB_env *mdb_env)
2020-02-08 14:09:34 +00:00
{
int err;
MDB_txn *mdb_txn;
MDB_dbi mdb_dbi;
MDB_val value;
if((err = mdb_txn_begin(mdb_env, NULL, 0, &mdb_txn)) != 0)
{
2020-04-13 22:50:55 +00:00
LOG_ERROR("mdb_txn_begin error %s", mdb_strerror(err));
2020-02-08 14:09:34 +00:00
exit(1);
}
2020-04-13 22:50:55 +00:00
if((err = mdb_dbi_open(mdb_txn, "controller", MDB_CREATE, &mdb_dbi)) != 0)
2020-02-08 14:09:34 +00:00
{
2020-04-13 22:50:55 +00:00
LOG_ERROR("mdb_dbi_open error %s", mdb_strerror(err));
2020-02-08 14:09:34 +00:00
exit(1);
}
2020-04-13 22:50:55 +00:00
value.mv_size = sizeof(uuid_t);
value.mv_data = controller->id;
if(controller_save_single(mdb_txn, mdb_dbi, DB_KEY_CONTROLLER_ID, value))
2020-02-08 14:09:34 +00:00
{
2020-04-13 22:50:55 +00:00
LOG_ERROR("failed to save ID");
2020-02-08 14:09:34 +00:00
return 1;
}
2020-04-13 22:50:55 +00:00
value.mv_size = sizeof(char) * (strlen(controller->name) + 1);
value.mv_data = controller->name;
if(controller_save_single(mdb_txn, mdb_dbi, DB_KEY_CONTROLLER_NAME, value))
2020-02-08 14:09:34 +00:00
{
2020-04-13 22:50:55 +00:00
LOG_ERROR("failed to save name");
2020-02-08 14:09:34 +00:00
return 1;
}
2020-04-13 22:50:55 +00:00
value.mv_size = sizeof(controller->command_port);
value.mv_data = &controller->command_port;
if(controller_save_single(mdb_txn, mdb_dbi, DB_KEY_CONTROLLER_COMMAND_PORT, value))
2020-02-09 23:58:17 +00:00
{
2020-04-13 22:50:55 +00:00
LOG_ERROR("failed to save command port");
2020-02-09 23:58:17 +00:00
return 1;
}
2020-04-13 22:50:55 +00:00
value.mv_size = sizeof(controller->discovery_port);
value.mv_data = &controller->discovery_port;
if(controller_save_single(mdb_txn, mdb_dbi, DB_KEY_CONTROLLER_DISCOVERY_PORT, value))
2020-02-08 14:09:34 +00:00
{
2020-04-13 22:50:55 +00:00
LOG_ERROR("failed to save discovery port");
2020-02-08 14:09:34 +00:00
return 1;
}
2020-04-13 22:50:55 +00:00
value.mv_size = sizeof(controller->relay_count);
value.mv_data = &controller->relay_count;
if(controller_save_single(mdb_txn, mdb_dbi, DB_KEY_CONTROLLER_RELAY_COUNT, value))
2020-02-08 14:09:34 +00:00
{
2020-04-13 22:50:55 +00:00
LOG_ERROR("failed to save relay count");
2020-02-08 14:09:34 +00:00
return 1;
}
mdb_txn_commit(mdb_txn);
2020-04-13 22:50:55 +00:00
for(uint8_t i = 0; i < controller->relay_count; ++i)
{
LOG_TRACE("saving relays[%d/%d]", i, controller->relay_count);
relay_save(controller->relays[i], mdb_env);
}
2020-02-08 14:09:34 +00:00
return 0;
}