controller-legacy/include/models/controller.h

108 lines
2.2 KiB
C
Raw Normal View History

2020-01-07 01:23:16 +00:00
#ifndef CONTROLLER_CONTROLLER_H
#define CONTROLLER_CONTROLLER_H
#include <uuid/uuid.h>
#include <stdint.h>
#include <lmdb.h>
#include <config.h>
#include <models/relay.h>
2020-02-08 14:09:34 +00:00
/**
* @brief Information about this controller
*/
2020-04-13 22:50:55 +00:00
typedef struct
2020-01-07 01:23:16 +00:00
{
int id;
2020-02-08 14:09:34 +00:00
/**
* @brief A unique UUID for this controller
*/
uuid_t uid;
2020-02-08 14:09:34 +00:00
/**
* @brief The name of this controller
*
* Includes a \0 terminator.
*/
2020-04-13 22:50:55 +00:00
char name[MAX_NAME_LENGTH + 1];
2020-02-09 23:58:17 +00:00
/**
* @brief The command port the controller was bound to
*/
uint16_t command_port;
2020-04-13 22:50:55 +00:00
relay_t **relays;
2020-01-07 01:23:16 +00:00
2020-04-13 22:50:55 +00:00
} controller_t;
2020-01-07 01:23:16 +00:00
2020-02-09 23:58:17 +00:00
/**
* @brief Key to save controller information in database
*/
2020-04-13 22:50:55 +00:00
typedef enum
2020-01-07 01:23:16 +00:00
{
2020-04-13 22:50:55 +00:00
DB_KEY_CONTROLLER_ID = 0,
DB_KEY_CONTROLLER_NAME = 1,
DB_KEY_CONTROLLER_COMMAND_PORT = 2,
DB_KEY_CONTROLLER_DISCOVERY_PORT = 3,
} db_key_controller_e;
2020-01-07 01:23:16 +00:00
2020-02-09 23:58:17 +00:00
/**
* @brief Create a new instance of controller
*
* This should not fail. The instance will be created with malloc and genric default values
*
* @return A new instance of #controller
*/
2020-04-13 22:50:55 +00:00
controller_t*
2020-01-07 01:23:16 +00:00
controller_create(void);
2020-02-09 23:58:17 +00:00
/**
* @brief Load a controller for database or create a new one
*
* Will return NULL when transaction can't start.
*
* @param mdb_env An opened MDB_env to load from
*
* @return A loaded or new instance of controller or NULL on database error
*/
2020-04-13 22:50:55 +00:00
controller_t*
controller_load();
2020-02-08 14:09:34 +00:00
/**
* @brief Save a controller to the database
*
2020-04-13 22:50:55 +00:00
* @param controller Instance of a controller
2020-02-08 14:09:34 +00:00
* @param mdb_env Already created MDB_env
*
* @return Indicator to show success (0) or failure (!0)
*/
2020-01-07 01:23:16 +00:00
int
controller_save();
2020-01-07 01:23:16 +00:00
2020-04-13 22:50:55 +00:00
/**
* @brief Sets a name to a controller.
* This function won't perform any checks (e.g. no NULL checks)
*
* @param controller Set the name to this controller
* @param name Name to be set
*/
void
2020-04-23 23:33:48 +00:00
controller_set_name(controller_t *controller, const char *name);
2020-04-13 22:50:55 +00:00
void
controller_free(controller_t *controller);
2020-02-09 23:58:17 +00:00
/**
* @brief Debug an instance of #controller
*
* Will use #LOG_DEBUG to log. So log will be depending on #LOG_LEVEL
*
* @param cntrlr #controller to debug
*/
2020-01-07 01:23:16 +00:00
void
2020-04-13 22:50:55 +00:00
controller_debug(controller_t *controller);
2020-01-07 01:23:16 +00:00
2020-06-24 09:41:12 +00:00
extern controller_t *global_controller;
2020-01-07 01:23:16 +00:00
#endif //CONTROLLER_CONTROLLER_H