init rewrite

This commit is contained in:
Tobias Reisinger 2020-05-05 11:42:02 +02:00
parent 9a44bc494e
commit 6d828fcffc
100 changed files with 50541 additions and 2707 deletions

18
include/colors.h Normal file
View file

@ -0,0 +1,18 @@
#ifndef CORE_COLORS_H
#define CORE_COLORS_H
#define COLOR_RED "\033[0;31m"
#define COLORB_RED "\033[1;31m"
#define COLOR_GREEN "\033[0;32m"
#define COLORB_GREEN "\033[1;32m"
#define COLOR_YELLOW "\033[0;33m"
#define COLORB_YELLOW "\033[1;33m"
#define COLOR_BLUE "\033[0;34m"
#define COLORB_BLUE "\033[1;34m"
#define COLOR_MAGENTA "\033[0;35m"
#define COLORB_MAGENTA "\033[1;35m"
#define COLOR_CYAN "\033[0;36m"
#define COLORB_CYAN "\033[1;36m"
#define COLOR_NONE "\033[0m"
#endif //CORE_COLORS_H

24
include/config.h Normal file
View file

@ -0,0 +1,24 @@
#ifndef CORE_CONFIG_H
#define CORE_CONFIG_H
#include <stdint.h>
#include <confini.h>
#include <enums.h>
typedef struct
{
char *file;
char *database;
log_level_t log_level;
run_type_t run_type;
char server_port[6];
uint16_t discovery_port;
} config_t;
extern config_t global_config;
int
config_load(IniDispatch *disp, void *config_void);
#endif /* CORE_CONFIG_H */

31
include/constants.h Normal file
View file

@ -0,0 +1,31 @@
#ifndef CORE_CONTANTS_H
#define CORE_CONTANTS_H
#define SECONDS_PER_DAY 86400 // 60 * 60 * 24
#define SECONDS_PER_MINUTE 60
#define POLL_FDS_COUNT 2
/**
* @brief Limit the maximum length of a controller/relay/etc name
*
* The NULL terminator is not included. Arrays of length #MAX_NAME_LENGTH + 1 are required.
*/
#define MAX_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
/**
* @brief How many milli seconds to wait until poll timeout in main loop
*/
#define ACCEPT_TIMEOUT_MSECONDS 1000
#define PIFACE_GPIO_BASE 200
#endif /* CORE_CONTANTS_H */

11
include/database.h Normal file
View file

@ -0,0 +1,11 @@
#ifndef CORE_DATABASE_H
#define CORE_DATABASE_H
#include <sqlite3.h>
extern sqlite3 *global_database;
int
database_migrate();
#endif /* CORE_DATABASE_H */

13
include/drivers.h Normal file
View file

@ -0,0 +1,13 @@
#ifndef CORE_DRIVERS_H
#define CORE_DRIVERS_H
#include <models/relay.h>
#include <enums.h>
void
driver_piface_set(int pin, int value);
void
driver_gpio_set(int pin, int value);
#endif /* CORE_DRIVERS_H */

View file

@ -0,0 +1,15 @@
#ifndef CORE_ENDPOINTS_API_V1_SCHEDULES_H
#define CORE_ENDPOINTS_API_V1_SCHEDULES_H
#include <router.h>
void
api_v1_schedules_POST(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
void
api_v1_schedules_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
void
api_v1_schedules_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
#endif /* CORE_ENDPOINTS_API_V1_SCHEDULES_H */

64
include/enums.h Normal file
View file

@ -0,0 +1,64 @@
#ifndef CORE_ENUMS_H
#define CORE_ENUMS_H
typedef enum
{
POLL_FDS_DISCOVERY,
POLL_FDS_COMMAND
} poll_fds_t;
typedef enum
{
DISCOVERY_MAPPING_ID = 0,
DISCOVERY_MAPPING_NAME = 1,
DISCOVERY_MAPPING_COMMAND_PORT = 2,
DISCOVERY_MAPPING_RELAY_COUNT = 3,
} discovery_mapping_t;
typedef enum
{
COMMAND_MAPPING_CODE = 0,
COMMAND_MAPPING_NAME = 1,
COMMAND_MAPPING_RELAY_NUM = 2,
COMMAND_MAPPING_SCHEDULES_ARRAY = 3,
COMMAND_MAPPING_SCHEDULE_ID = 4,
COMMAND_MAPPING_PERIODS_COUNT = 5,
COMMAND_MAPPING_PERIODS_BLOB = 6,
} control_mapping_t;
typedef enum
{
COMMAND_CODE_GET_TIME = 1,
COMMAND_CODE_GET_ID = 2,
COMMAND_CODE_SET_NAME = 100,
COMMAND_CODE_GET_NAME = 101,
COMMAND_CODE_SET_SCHEDULE = 102,
COMMAND_CODE_GET_SCHEDULE = 103,
COMMAND_CODE_SET_RELAY_NAME = 104,
COMMAND_CODE_GET_RELAY_NAME = 105,
} command_code_t;
typedef enum
{
RELAY_DRIVER_NONE,
RELAY_DRIVER_GPIO,
RELAY_DRIVER_PIFACE,
} relay_driver_t;
typedef enum
{
RUN_TYPE_START,
RUN_TYPE_TEST,
} run_type_t;
typedef enum
{
LOG_LEVEL_TRACE = 5,
LOG_LEVEL_DEBUG = 4,
LOG_LEVEL_INFO = 3,
LOG_LEVEL_WARN = 2,
LOG_LEVEL_ERROR = 1,
LOG_LEVEL_FATAL = 0,
} log_level_t;
#endif /* CORE_ENUMS_H */

7
include/handlers.h Normal file
View file

@ -0,0 +1,7 @@
#ifndef CORE_HANDLERS_H
#define CORE_HANDLERS_H
void
handler_connection(struct mg_connection *c, int ev, void *p);
#endif /* CORE_HANDLERS_H */

34
include/helpers.h Normal file
View file

@ -0,0 +1,34 @@
#ifndef CORE_HELPERS_H
#define CORE_HELPERS_H
#include <config.h>
#include <confini.h>
int
helper_connect_tcp_server(char* host, uint16_t port);
int
helper_bind_tcp_server(char* addr, uint16_t port, int max_client_backlog);
uint16_t
helper_get_port(int sock);
/**
* @brief Open socket for discovery
*
* Will exit program when unable to open socket.
*
* @param discovery_port Port number to listen on for discovery broadcasts
*
* @return Open socket to accept discovery broadcasts on
*/
int
helper_open_discovery_socket(uint16_t discovery_port);
void
helper_parse_cli(int argc, const char **argv, config_t *config);
int
helper_get_weekday(const time_t timestamp_now);
#endif /* CORE_HELPERS_H */

21
include/logger.h Normal file
View file

@ -0,0 +1,21 @@
#ifndef CORE_LOGGER_H
#define CORE_LOGGER_H
#include <stdio.h>
#include <time.h>
#include <colors.h>
#include <config.h>
#include <macros.h>
void
logger_log(FILE *stream, log_level_t level, const char *filename, int line, const char *func, const char *msg, ...);
#define LOG_TRACE(...) logger_log(stdout, LOG_LEVEL_TRACE, __FILENAME__, __LINE__, __func__, ##__VA_ARGS__)
#define LOG_DEBUG(...) logger_log(stdout, LOG_LEVEL_DEBUG, __FILENAME__, __LINE__, __func__, ##__VA_ARGS__)
#define LOG_INFO(...) logger_log(stdout, LOG_LEVEL_INFO , __FILENAME__, __LINE__, __func__, ##__VA_ARGS__)
#define LOG_WARN(...) logger_log(stdout, LOG_LEVEL_WARN , __FILENAME__, __LINE__, __func__, ##__VA_ARGS__)
#define LOG_ERROR(...) logger_log(stderr, LOG_LEVEL_ERROR, __FILENAME__, __LINE__, __func__, ##__VA_ARGS__)
#define LOG_FATAL(...) logger_log(stderr, LOG_LEVEL_FATAL, __FILENAME__, __LINE__, __func__, ##__VA_ARGS__)
#endif //CORE_LOGGER_H

13
include/macros.h Normal file
View file

@ -0,0 +1,13 @@
#ifndef CORE_MACROS_H
#define CORE_MACROS_H
#include <colors.h>
#include <logger.h>
#ifndef SOURCE_PATH_SIZE
#define SOURCE_PATH_SIZE 0
#endif
#define __FILENAME__ (__FILE__ + SOURCE_PATH_SIZE)
#endif //CORE_MACROS_H

View file

@ -0,0 +1,45 @@
#ifndef CORE_MODELS_CONTROLLER_H
#define CORE_MODELS_CONTROLLER_H
#include <uuid/uuid.h>
#include <sqlite3.h>
#include <helpers.h>
#include <models/relay.h>
typedef struct
{
uuid_t id;
char name[128];
char ip[17];
int active;
int port;
int relay_count;
relay_t **relays;
} controller_t;
void
controller_free(controller_t* contoller);
int
controller_save(controller_t* contoller);
int
controller_remove(controller_t* contoller);
char*
controller_to_json(controller_t* contoller);
controller_t**
controller_get_by_simple(const char *key, const void *value, intptr_t bind_func, int bind_func_param);
controller_t**
controller_get_all();
int
controller_command(int command_code, char *payload, uint32_t payload_size);
void
controller_free_list(controller_t **controllers_list);
#endif /* CORE_MODELS_CONTROLLER_H */

View file

@ -0,0 +1,19 @@
#ifndef CORE_MODELS_JUNCTION_RELAY_SCHEDULE_H
#define CORE_MODELS_JUNCTION_RELAY_SCHEDULE_H
int
junction_relay_schedule_get_schedule_id(uint8_t weekday, int relay_id);
int
junction_relay_schedule_insert(uint8_t weekday, int relay_id, int schedule_id);
int
junction_relay_schedule_remove(uint8_t weekday, int relay_id, int schedule_id);
int
junction_relay_schedule_remove_for_relay(int relay_id);
int
junction_relay_schedule_remove_for_schedule(int schedule_id);
#endif /* CORE_MODELS_JUNCTION_RELAY_SCHEDULE_H */

View file

@ -0,0 +1,32 @@
#ifndef CORE_MODELS_JUNCTION_TAG_H
#define CORE_MODELS_JUNCTION_TAG_H
int*
junction_tag_get_relays_for_tag_id(int tag_id);
int*
junction_tag_get_schedules_for_tag_id(int tag_id);
int*
junction_tag_get_tags_for_relay_id(int relay_id);
int*
junction_tag_get_tags_for_schedule_id(int schedule_id);
int
junction_tag_insert(int tag_id, int relay_id, int schedule_id);
int
junction_tag_remove(int tag_id, int relay_id, int schedule_id);
int
junction_tag_remove_for_tag(int tag_id);
int
junction_tag_remove_for_relay(int relay_id);
int
junction_tag_remove_for_schedule(int schedule_id);
#endif /* CORE_MODELS_JUNCTION_TAG_H */

21
include/models/period.h Normal file
View file

@ -0,0 +1,21 @@
#ifndef CORE_PERIOD_H
#define CORE_PERIOD_H
#include <stdint.h>
typedef struct
{
uint16_t start;
uint16_t end;
} period_t;
period_t*
period_create(uint16_t start, uint16_t end);
int
period_includes_time(period_t *period, uint16_t timestamp);
int
period_helper_parse_hhmm(const char *hhmm_str, uint16_t *hhmm);
#endif /* CORE_PERIOD_H */

49
include/models/relay.h Normal file
View file

@ -0,0 +1,49 @@
#ifndef CORE_RELAY_H
#define CORE_RELAY_H
#include <string.h>
#include <uuid/uuid.h>
#include <helpers.h>
#include <database.h>
#include <models/schedule.h>
typedef struct
{
int id;
char name[128];
int number;
uuid_t controller_id;
int active_schedule_id;
schedule_t *active_schedule;
schedule_t *schedules[7];
} relay_t;
bool
relay_save();
bool
relay_remove();
char*
relay_to_json();
void
relay_free_list(relay_t **relays_list);
relay_t**
relay_get_by_simple(const char *key, const void *value, intptr_t bind_func, int bind_func_param);
relay_t*
relay_get_by_id(int id);
relay_t*
relay_get_relay_for_controller(uuid_t controller_id, int relay_num);
bool
relay_valid_num_is_for_controller(uuid_t controller_id, int relay_num);
relay_t**
relay_get_all();
#endif /* CORE_RELAY_H */

58
include/models/schedule.h Normal file
View file

@ -0,0 +1,58 @@
#ifndef CORE_SCHEDULE_H
#define CORE_SCHEDULE_H
#include <uuid/uuid.h>
#include <cJSON.h>
#include <constants.h>
#include <models/period.h>
typedef struct
{
int id;
uuid_t uid;
char name[MAX_NAME_LENGTH + 1];
uint16_t periods_count;
period_t *periods;
} schedule_t;
int
schedule_save(schedule_t *schedule);
int
schedule_remove(schedule_t *schedule);
void
schedule_free(schedule_t *schedule);
void
schedule_free_list(schedule_t **schedule);
cJSON*
schedule_to_json(schedule_t *schedule);
void
schedule_free_list(schedule_t **schedules_list);
uint16_t*
schedule_periods_to_blob(schedule_t *schedule);
schedule_t**
schedule_get_by_simple(const char *key, const void *value, intptr_t bind_func, int bind_func_param);
schedule_t*
schedule_get_by_id_or_off(int id);
schedule_t*
schedule_get_by_id(int id);
schedule_t**
schedule_get_all();
int
schedule_uid_parse(const char *uid_str, uuid_t result);
void
schedule_uid_unparse(const uuid_t uid, char *result);
#endif /* CORE_SCHEDULE_H */

17
include/models/tag.h Normal file
View file

@ -0,0 +1,17 @@
#ifndef CORE_MODELS_TAG_H
#define CORE_MODELS_TAG_H
int
tag_save(int id, const char *tag);
int
tag_remove(int id);
char*
tag_get_tag(int id);
int
tag_get_id(const char* tag);
#endif /* CORE_MODELS_TAG_H */

52
include/router.h Normal file
View file

@ -0,0 +1,52 @@
#ifndef CORE_ROUTER_H
#define CORE_ROUTER_H
#include <mongoose.h>
#define ENDPOINTS_MAX_COUNT 16
typedef enum
{
ENDPOINT_ARG_TYPE_INT,
ENDPOINT_ARG_TYPE_STR
} endpoint_arg_type_e;
typedef struct
{
endpoint_arg_type_e type;
union
{
int v_int;
char *v_str;
} value;
} endpoint_args_t;
typedef void (*endpoint_func_f)(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm);
typedef struct
{
char **route;
char *route_keeper;
int methods;
endpoint_func_f func;
int args_count;
endpoint_args_t *args;
int possible_route;
int args_found;
} endpoint_t;
void
router_init();
void
router_register_endpoint(const char *route, int methods, endpoint_func_f func);
endpoint_t*
router_find_endpoint(const char *uri_str, size_t uri_len, struct mg_str *method);
void
router_free();
#endif /* CORE_ROUTER_H */