add: much
This commit is contained in:
parent
7b6ee283c6
commit
cbb4ac7a86
15 changed files with 159 additions and 56 deletions
|
@ -11,6 +11,7 @@ string(LENGTH "${CMAKE_SOURCE_DIR}/" SOURCE_PATH_SIZE)
|
||||||
add_definitions("-DSOURCE_PATH_SIZE=${SOURCE_PATH_SIZE}")
|
add_definitions("-DSOURCE_PATH_SIZE=${SOURCE_PATH_SIZE}")
|
||||||
|
|
||||||
if(WIRING_PI_DEBUG)
|
if(WIRING_PI_DEBUG)
|
||||||
|
message("Using wiringPi debug calls")
|
||||||
add_compile_definitions("WIRING_PI_DEBUG")
|
add_compile_definitions("WIRING_PI_DEBUG")
|
||||||
endif(WIRING_PI_DEBUG)
|
endif(WIRING_PI_DEBUG)
|
||||||
|
|
||||||
|
@ -25,3 +26,7 @@ add_custom_target(run
|
||||||
DEPENDS controller
|
DEPENDS controller
|
||||||
WORKING_DIRECTORY ${CMAKE_PROJECT_DIR}
|
WORKING_DIRECTORY ${CMAKE_PROJECT_DIR}
|
||||||
)
|
)
|
||||||
|
add_custom_target(docs
|
||||||
|
COMMAND doxygen
|
||||||
|
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
|
||||||
|
)
|
||||||
|
|
2
Doxyfile
2
Doxyfile
|
@ -1708,7 +1708,7 @@ EXTRA_SEARCH_MAPPINGS =
|
||||||
# If the GENERATE_LATEX tag is set to YES, doxygen will generate LaTeX output.
|
# If the GENERATE_LATEX tag is set to YES, doxygen will generate LaTeX output.
|
||||||
# The default value is: YES.
|
# The default value is: YES.
|
||||||
|
|
||||||
GENERATE_LATEX = YES
|
GENERATE_LATEX = NO
|
||||||
|
|
||||||
# The LATEX_OUTPUT tag is used to specify where the LaTeX docs will be put. If a
|
# The LATEX_OUTPUT tag is used to specify where the LaTeX docs will be put. If a
|
||||||
# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of
|
# relative path is entered the value of OUTPUT_DIRECTORY will be put in front of
|
||||||
|
|
30
database.c
Normal file
30
database.c
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <lmdb.h>
|
||||||
|
|
||||||
|
#include <config.h>
|
||||||
|
|
||||||
|
void
|
||||||
|
database_setup(MDB_env **mdb_env)
|
||||||
|
{
|
||||||
|
int err;
|
||||||
|
|
||||||
|
if(mdb_env_create(mdb_env) != 0)
|
||||||
|
{
|
||||||
|
perror("Can't create mdb handle");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if((err = mdb_env_set_maxdbs(*mdb_env, MDB_MAXDBS)) != 0)
|
||||||
|
{
|
||||||
|
fprintf(stderr, "mdb_env_set_maxdbs error %s\n", mdb_strerror(err));
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(mdb_env_open(*mdb_env, "db.lmdb", MDB_NOSUBDIR, 0700) != 0)
|
||||||
|
{
|
||||||
|
perror("Can't open mdb file");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,17 +3,25 @@
|
||||||
|
|
||||||
#include <log_levels.h>
|
#include <log_levels.h>
|
||||||
|
|
||||||
#define CONTROLLER_FILE_NAME "emgauwa-controller.dat"
|
/**
|
||||||
#define CONTROLLER_FILE_VERSION 1
|
* @brief Limit the maximum length of a controller name
|
||||||
#define CONTROLLER_FILE_HEADER_SIZE 4
|
*
|
||||||
|
* The NULL terminator is not included. Arrays of length #CONTROLLER_NAME_LENGTH + 1 are required.
|
||||||
|
*/
|
||||||
#define CONTROLLER_NAME_LENGTH 128
|
#define CONTROLLER_NAME_LENGTH 128
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Maximum number of dbs for the databases for the MDB_env
|
||||||
|
*
|
||||||
|
* Used when calling mdb_env_set_maxdbs() in database_setup()
|
||||||
|
*/
|
||||||
#define MDB_MAXDBS 8
|
#define MDB_MAXDBS 8
|
||||||
|
|
||||||
#define TEST_KEY "blubb"
|
/**
|
||||||
#define TEST_VALUE "bla bla bla"
|
* @brief Indicates to which level to log
|
||||||
|
*
|
||||||
|
* @see include/log_levels.h
|
||||||
|
*/
|
||||||
#define LOG_LEVEL LOG_LEVEL_TRACE
|
#define LOG_LEVEL LOG_LEVEL_TRACE
|
||||||
|
|
||||||
#endif //CONTROLLER_CONFIG_H
|
#endif //CONTROLLER_CONFIG_H
|
||||||
|
|
17
include/database.h
Normal file
17
include/database.h
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
#ifndef CONTROLLER_DATABASE_H
|
||||||
|
#define CONTROLLER_DATABASE_H
|
||||||
|
|
||||||
|
#include <lmdb.h>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Setup lmdb database enviroment
|
||||||
|
*
|
||||||
|
* Creates, sets max dbs and opens an MDB_env instance.
|
||||||
|
* Will return on success, but exit program on failure.
|
||||||
|
*
|
||||||
|
* @param mdb_env Source variable will be set to new MDB_env
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
database_setup(MDB_env **mdb_env);
|
||||||
|
|
||||||
|
#endif /* CONTROLLER_DATABASE_H */
|
4
include/discovery.h
Normal file
4
include/discovery.h
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
#ifndef CONTROLLER_DISCOVERY_H
|
||||||
|
#define CONTROLLER_DISCOVERY_H
|
||||||
|
|
||||||
|
#endif /* CONTROLLER_DISCOVERY_H */
|
|
@ -1,4 +0,0 @@
|
||||||
#include <stdint.h>
|
|
||||||
|
|
||||||
void
|
|
||||||
display_setup(void);
|
|
|
@ -1,5 +1,5 @@
|
||||||
#ifndef CONTROLLER_LOG_LEVEL_H
|
#ifndef CONTROLLER_LOG_LEVELS_H
|
||||||
#define CONTROLLER_LOG_LEVEL_H
|
#define CONTROLLER_LOG_LEVELS_H
|
||||||
|
|
||||||
#define LOG_LEVEL_TRACE 5
|
#define LOG_LEVEL_TRACE 5
|
||||||
#define LOG_LEVEL_DEBUG 4
|
#define LOG_LEVEL_DEBUG 4
|
||||||
|
@ -8,4 +8,4 @@
|
||||||
#define LOG_LEVEL_ERROR 1
|
#define LOG_LEVEL_ERROR 1
|
||||||
#define LOG_LEVEL_FATAL 0
|
#define LOG_LEVEL_FATAL 0
|
||||||
|
|
||||||
#endif //CONTROLLER_LOG_LEVEL_H
|
#endif //CONTROLLER_LOG_LEVELS_H
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#ifndef CONTROLLER_LOGGER_H
|
#ifndef CONTROLLER_LOGGER_H
|
||||||
#define CONTROLLER_LOGGER_H
|
#define CONTROLLER_LOGGER_H
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
#include <colors.h>
|
#include <colors.h>
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
#include <macros.h>
|
#include <macros.h>
|
||||||
|
|
|
@ -23,24 +23,55 @@ typedef struct controller
|
||||||
* Includes a \0 terminator.
|
* Includes a \0 terminator.
|
||||||
*/
|
*/
|
||||||
char name[CONTROLLER_NAME_LENGTH + 1];
|
char name[CONTROLLER_NAME_LENGTH + 1];
|
||||||
uint16_t port;
|
/**
|
||||||
|
* @brief The command port the controller was bound to
|
||||||
|
*/
|
||||||
|
uint16_t command_port;
|
||||||
|
/**
|
||||||
|
* @brief The discovery port the controller receives broadcasts on
|
||||||
|
*/
|
||||||
|
uint16_t discovery_port;
|
||||||
|
/**
|
||||||
|
* @brief Amount of relays available to this controller
|
||||||
|
*/
|
||||||
uint8_t relay_count;
|
uint8_t relay_count;
|
||||||
relay **relays;
|
relay **relays;
|
||||||
|
|
||||||
} controller;
|
} controller;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Key to save controller information in database
|
||||||
|
*/
|
||||||
typedef enum controller_db_key
|
typedef enum controller_db_key
|
||||||
{
|
{
|
||||||
KEY_META_UUID,
|
KEY_META_UUID = 0,
|
||||||
KEY_META_NAME,
|
KEY_META_NAME = 1,
|
||||||
KEY_META_PORT,
|
KEY_META_COMMAND_PORT = 2,
|
||||||
KEY_META_RELAY_COUNT,
|
KEY_META_DISCOVERY_PORT = 3,
|
||||||
KEY_META_RELAYS
|
KEY_META_RELAY_COUNT = 4,
|
||||||
|
KEY_META_RELAYS = 5,
|
||||||
} controller_db_key;
|
} controller_db_key;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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
|
||||||
|
*/
|
||||||
controller*
|
controller*
|
||||||
controller_create(void);
|
controller_create(void);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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
|
||||||
|
*/
|
||||||
controller*
|
controller*
|
||||||
controller_load(MDB_env *mdb_env);
|
controller_load(MDB_env *mdb_env);
|
||||||
|
|
||||||
|
@ -55,6 +86,14 @@ controller_load(MDB_env *mdb_env);
|
||||||
int
|
int
|
||||||
controller_save(controller *cntrlr, MDB_env *mdb_env);
|
controller_save(controller *cntrlr, MDB_env *mdb_env);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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
|
||||||
|
*/
|
||||||
void
|
void
|
||||||
controller_debug(controller *cntrlr);
|
controller_debug(controller *cntrlr);
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
|
#ifndef CONTROLLER_WIRING_DEBUG_H
|
||||||
|
#define CONTROLLER_WIRING_DEBUG_H
|
||||||
|
|
||||||
#ifdef WIRING_PI_DEBUG
|
#ifdef WIRING_PI_DEBUG
|
||||||
|
#define wiringPiSetup() LOG_INFO("wiringP wiringPiSetup()")
|
||||||
#define wiringPiSetup() printf("Setting up wiringPi....\n")
|
#define pinMode(x,y) LOG_INFO("wiringPi pinMode(%d, %d)", x, y)
|
||||||
#define pinMode(x,y) printf("pinMode(%d, %d)\n",x,y)
|
#define digitalWrite(x,y) LOG_INFO("wiringPi digitalWrite(%d, %d)", x, y)
|
||||||
#define digitalWrite(x,y) printf("digitalWrite(%d, %d)\n",x,y)
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#endif /* CONTROLLER_WIRING_DEBUG_H */
|
||||||
|
|
22
main.c
22
main.c
|
@ -5,6 +5,8 @@
|
||||||
|
|
||||||
#include <logger.h>
|
#include <logger.h>
|
||||||
#include <models/controller.h>
|
#include <models/controller.h>
|
||||||
|
#include <database.h>
|
||||||
|
#include <config.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief The main function
|
* @brief The main function
|
||||||
|
@ -20,26 +22,8 @@ main(int argc, char** argv)
|
||||||
(void)argc;
|
(void)argc;
|
||||||
(void)argv;
|
(void)argv;
|
||||||
|
|
||||||
int err;
|
|
||||||
|
|
||||||
MDB_env *mdb_env;
|
MDB_env *mdb_env;
|
||||||
|
database_setup(&mdb_env);
|
||||||
if(mdb_env_create(&mdb_env) != 0)
|
|
||||||
{
|
|
||||||
perror("Can't create mdb handle");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if((err = mdb_env_set_maxdbs(mdb_env, MDB_MAXDBS)) != 0)
|
|
||||||
{
|
|
||||||
fprintf(stderr, "mdb_env_set_maxdbs error %s\n", mdb_strerror(err));
|
|
||||||
}
|
|
||||||
|
|
||||||
if(mdb_env_open(mdb_env, "db.lmdb", MDB_NOSUBDIR, 0700) != 0)
|
|
||||||
{
|
|
||||||
perror("Can't open mdb file");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
controller *this_cntrlr = controller_load(mdb_env);
|
controller *this_cntrlr = controller_load(mdb_env);
|
||||||
controller_save(this_cntrlr, mdb_env);
|
controller_save(this_cntrlr, mdb_env);
|
||||||
|
|
|
@ -14,7 +14,8 @@ controller_create(void)
|
||||||
uuid_generate(result->uuid);
|
uuid_generate(result->uuid);
|
||||||
|
|
||||||
strcpy(result->name, "new emgauwa device");
|
strcpy(result->name, "new emgauwa device");
|
||||||
result->port = 0;
|
result->command_port = 0;
|
||||||
|
result->discovery_port = 4419;
|
||||||
result->relay_count = 10;
|
result->relay_count = 10;
|
||||||
|
|
||||||
result->relays = malloc(sizeof(*result->relays) * result->relay_count);
|
result->relays = malloc(sizeof(*result->relays) * result->relay_count);
|
||||||
|
@ -30,9 +31,14 @@ controller_create(void)
|
||||||
void
|
void
|
||||||
controller_debug(controller *cntrlr)
|
controller_debug(controller *cntrlr)
|
||||||
{
|
{
|
||||||
|
if(cntrlr == NULL)
|
||||||
|
{
|
||||||
|
LOG_DEBUG("controller is NULL");
|
||||||
|
}
|
||||||
char uuid_str[37];
|
char uuid_str[37];
|
||||||
uuid_unparse(cntrlr->uuid, uuid_str);
|
uuid_unparse(cntrlr->uuid, uuid_str);
|
||||||
printf("%s @ %p\n", uuid_str, cntrlr);
|
LOG_DEBUG("(1/4) %s @ %p", uuid_str, cntrlr);
|
||||||
printf("name: %s\n", cntrlr->name);
|
LOG_DEBUG("(2/4) name: %s", cntrlr->name);
|
||||||
printf("port: %5d relays: %3d\n", cntrlr->port, cntrlr->relay_count);
|
LOG_DEBUG("(3/4) relays: %3d", cntrlr->relay_count);
|
||||||
|
LOG_DEBUG("(4/4) command_port: %5d discovery_port: %5d", cntrlr->command_port, cntrlr->discovery_port);
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,7 +35,7 @@ controller_load(MDB_env *mdb_env)
|
||||||
if((err = mdb_txn_begin(mdb_env, NULL, MDB_RDONLY, &mdb_txn)) != 0)
|
if((err = mdb_txn_begin(mdb_env, NULL, MDB_RDONLY, &mdb_txn)) != 0)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "mdb_txn_begin error %s\n", mdb_strerror(err));
|
fprintf(stderr, "mdb_txn_begin error %s\n", mdb_strerror(err));
|
||||||
exit(1);
|
return NULL;
|
||||||
}
|
}
|
||||||
if((err = mdb_dbi_open(mdb_txn, "meta", 0, &mdb_dbi)) != 0)
|
if((err = mdb_dbi_open(mdb_txn, "meta", 0, &mdb_dbi)) != 0)
|
||||||
{
|
{
|
||||||
|
@ -48,7 +48,7 @@ controller_load(MDB_env *mdb_env)
|
||||||
return new_controller;
|
return new_controller;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "mdb_txn_begin error %s\n", mdb_strerror(err));
|
fprintf(stderr, "mdb_txn_begin error %s\n", mdb_strerror(err));
|
||||||
exit(1);
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -63,8 +63,11 @@ controller_load(MDB_env *mdb_env)
|
||||||
strncpy(new_controller->name, (char*)value.mv_data, CONTROLLER_NAME_LENGTH);
|
strncpy(new_controller->name, (char*)value.mv_data, CONTROLLER_NAME_LENGTH);
|
||||||
new_controller->name[CONTROLLER_NAME_LENGTH] = '\0';
|
new_controller->name[CONTROLLER_NAME_LENGTH] = '\0';
|
||||||
|
|
||||||
controller_load_single(mdb_txn, mdb_dbi, KEY_META_PORT, &value);
|
controller_load_single(mdb_txn, mdb_dbi, KEY_META_COMMAND_PORT, &value);
|
||||||
new_controller->port = ((uint16_t*)value.mv_data)[0];
|
new_controller->command_port = ((uint16_t*)value.mv_data)[0];
|
||||||
|
|
||||||
|
controller_load_single(mdb_txn, mdb_dbi, KEY_META_DISCOVERY_PORT, &value);
|
||||||
|
new_controller->discovery_port = ((uint16_t*)value.mv_data)[0];
|
||||||
|
|
||||||
controller_load_single(mdb_txn, mdb_dbi, KEY_META_RELAY_COUNT, &value);
|
controller_load_single(mdb_txn, mdb_dbi, KEY_META_RELAY_COUNT, &value);
|
||||||
new_controller->relay_count = ((uint8_t*)value.mv_data)[0];
|
new_controller->relay_count = ((uint8_t*)value.mv_data)[0];
|
||||||
|
|
|
@ -62,9 +62,16 @@ controller_save(controller *cntrlr, MDB_env *mdb_env)
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
value.mv_size = sizeof(cntrlr->port);
|
value.mv_size = sizeof(cntrlr->command_port);
|
||||||
value.mv_data = &cntrlr->port;
|
value.mv_data = &cntrlr->command_port;
|
||||||
if(controller_save_single(mdb_txn, mdb_dbi, KEY_META_PORT, value))
|
if(controller_save_single(mdb_txn, mdb_dbi, KEY_META_COMMAND_PORT, value))
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
value.mv_size = sizeof(cntrlr->discovery_port);
|
||||||
|
value.mv_data = &cntrlr->discovery_port;
|
||||||
|
if(controller_save_single(mdb_txn, mdb_dbi, KEY_META_DISCOVERY_PORT, value))
|
||||||
{
|
{
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue