add: relay/piface support
This commit is contained in:
parent
fa6ceb2bf4
commit
db64e4f820
34 changed files with 1259 additions and 313 deletions
|
@ -4,11 +4,11 @@
|
|||
#include <log_levels.h>
|
||||
|
||||
/**
|
||||
* @brief Limit the maximum length of a controller name
|
||||
* @brief Limit the maximum length of a controller/relay/etc name
|
||||
*
|
||||
* The NULL terminator is not included. Arrays of length #CONTROLLER_NAME_LENGTH + 1 are required.
|
||||
* The NULL terminator is not included. Arrays of length #MAX_NAME_LENGTH + 1 are required.
|
||||
*/
|
||||
#define CONTROLLER_NAME_LENGTH 128
|
||||
#define MAX_NAME_LENGTH 128
|
||||
|
||||
/**
|
||||
* @brief Maximum number of dbs for the databases for the MDB_env
|
||||
|
|
3
include/constants.h
Normal file
3
include/constants.h
Normal file
|
@ -0,0 +1,3 @@
|
|||
#define SECONDS_PER_DAY 86400 // 60 * 60 * 24
|
||||
|
||||
#define SECONDS_PER_MINUTE 60
|
|
@ -1,27 +0,0 @@
|
|||
#ifndef CONTROLLER_DISCOVERY_H
|
||||
#define CONTROLLER_DISCOVERY_H
|
||||
|
||||
#include <models/controller.h>
|
||||
|
||||
/**
|
||||
* @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
|
||||
discovery_socket_open(uint16_t discovery_port);
|
||||
|
||||
/**
|
||||
* @brief Handle the discovery processing
|
||||
*
|
||||
* @param fd File descriptor to receive initial data from
|
||||
* @param cntrlr Controller to use for answering discovery
|
||||
*/
|
||||
void
|
||||
discovery_handle_discover(int fd, controller *cntrlr);
|
||||
|
||||
#endif /* CONTROLLER_DISCOVERY_H */
|
14
include/drivers.h
Normal file
14
include/drivers.h
Normal file
|
@ -0,0 +1,14 @@
|
|||
#ifndef CONTROLLER_DRIVERS_H
|
||||
#define CONTROLLER_DRIVERS_H
|
||||
|
||||
#include <models/relay.h>
|
||||
|
||||
#define DRIVER_PIFACE_GPIO_BASE 200
|
||||
|
||||
void
|
||||
driver_piface_set(relay_t *relay, int value);
|
||||
|
||||
void
|
||||
driver_gpio_set(relay_t *relay, int value);
|
||||
|
||||
#endif /* CONTROLLER_DRIVERS_H */
|
|
@ -1,8 +1,40 @@
|
|||
#ifndef CONTROLLER_ENUMS_H
|
||||
#define CONTROLLER_ENUMS_H
|
||||
|
||||
enum poll_fgs {
|
||||
POLL_FGS_DISCOVERY
|
||||
enum poll_fgs
|
||||
{
|
||||
POLL_FGS_DISCOVERY,
|
||||
POLL_FGS_COMMAND
|
||||
};
|
||||
|
||||
enum discovery_mapping
|
||||
{
|
||||
DISCOVERY_MAPPING_ID = 0,
|
||||
DISCOVERY_MAPPING_NAME = 1,
|
||||
DISCOVERY_MAPPING_COMMAND_PORT = 2,
|
||||
DISCOVERY_MAPPING_RELAY_COUNT = 3,
|
||||
};
|
||||
|
||||
enum control_mapping
|
||||
{
|
||||
COMMAND_MAPPING_CODE = 0,
|
||||
COMMAND_MAPPING_NAME = 1,
|
||||
COMMAND_MAPPING_RELAY_NUM = 2,
|
||||
COMMAND_MAPPING_SCHEDULE_ID = 3,
|
||||
COMMAND_MAPPING_PERIODS_COUNT = 4,
|
||||
COMMAND_MAPPING_PERIODS_BLOB = 5,
|
||||
};
|
||||
|
||||
enum command_code
|
||||
{
|
||||
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,
|
||||
};
|
||||
|
||||
#endif /* CONTROLLER_ENUMS_H */
|
||||
|
|
24
include/handlers.h
Normal file
24
include/handlers.h
Normal file
|
@ -0,0 +1,24 @@
|
|||
#ifndef CONTROLLER_HANDLERS_H
|
||||
#define CONTROLLER_HANDLERS_H
|
||||
|
||||
#include <models/controller.h>
|
||||
|
||||
/**
|
||||
* @brief Handle the command processing
|
||||
*
|
||||
* @param fd File descriptor to receive initial data from
|
||||
* @param controller Controller to use for answering command
|
||||
*/
|
||||
void
|
||||
handler_command(int fd, controller_t *controller);
|
||||
|
||||
/**
|
||||
* @brief Handle the discovery processing
|
||||
*
|
||||
* @param fd File descriptor to receive initial data from
|
||||
* @param controller Controller to use for answering discovery
|
||||
*/
|
||||
void
|
||||
handler_discovery(int fd, controller_t *controller);
|
||||
|
||||
#endif /* CONTROLLER_HANDLERS_H */
|
|
@ -1,7 +0,0 @@
|
|||
#ifndef CONTROLLER_HELPER_H
|
||||
#define CONTROLLER_HELPER_H
|
||||
|
||||
int
|
||||
helper_connect_server(char* host, char* port);
|
||||
|
||||
#endif /* CONTROLLER_HELPER_H */
|
25
include/helpers.h
Normal file
25
include/helpers.h
Normal file
|
@ -0,0 +1,25 @@
|
|||
#ifndef CONTROLLER_HELPERS_H
|
||||
#define CONTROLLER_HELPERS_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);
|
||||
|
||||
#endif /* CONTROLLER_HELPERS_H */
|
|
@ -2,45 +2,52 @@
|
|||
#define CONTROLLER_LOGGER_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <colors.h>
|
||||
#include <config.h>
|
||||
#include <macros.h>
|
||||
|
||||
#define _LOGGER_MESSAGE(msg) COLOR_NONE " %s:%s:%d: " msg "\n", __FILENAME__, __func__, __LINE__
|
||||
#define _LOGGER_TIMESTAMP_SIZE 32
|
||||
char _LOGGER_TIMESTAMP[_LOGGER_TIMESTAMP_SIZE];
|
||||
|
||||
char*
|
||||
logger_get_timestamp();
|
||||
|
||||
#define _LOGGER_MESSAGE(msg) COLOR_NONE " %s %s:%d:%s: " msg "\n", logger_get_timestamp(), __FILENAME__, __LINE__, __func__
|
||||
|
||||
#if LOG_LEVEL >= LOG_LEVEL_TRACE
|
||||
#define LOG_TRACE(msg, ...) printf(COLOR_GREEN "[TRACE]" _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
|
||||
#define LOG_TRACE(msg, ...) fprintf(stdout, COLOR_GREEN "[TRACE]" _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
|
||||
#else
|
||||
#define LOG_TRACE(msg, ...)
|
||||
#endif
|
||||
|
||||
#if LOG_LEVEL >= LOG_LEVEL_DEBUG
|
||||
#define LOG_DEBUG(msg, ...) printf(COLOR_BLUE "[DEBUG]" _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
|
||||
#define LOG_DEBUG(msg, ...) fprintf(stdout, COLOR_BLUE "[DEBUG]" _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
|
||||
#else
|
||||
#define LOG_DEBUG(msg, ...)
|
||||
#endif
|
||||
|
||||
#if LOG_LEVEL >= LOG_LEVEL_INFO
|
||||
#define LOG_INFO(msg, ...) printf(COLOR_CYAN "[ INFO] " _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
|
||||
#define LOG_INFO(msg, ...) fprintf(stdout, COLOR_CYAN "[ INFO]" _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
|
||||
#else
|
||||
#define LOG_INFO(msg, ...)
|
||||
#endif
|
||||
|
||||
#if LOG_LEVEL >= LOG_LEVEL_WARN
|
||||
#define LOG_WARN(msg, ...) printf(COLOR_YELLOW "[ WARN] " _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
|
||||
#define LOG_WARN(msg, ...) fprintf(stdout, COLOR_YELLOW "[ WARN]" _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
|
||||
#else
|
||||
#define LOG_WARN(msg, ...)
|
||||
#endif
|
||||
|
||||
#if LOG_LEVEL >= LOG_LEVEL_ERROR
|
||||
#define LOG_ERROR(msg, ...) printf(COLOR_RED "[ERROR]" _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
|
||||
#define LOG_ERROR(msg, ...) fprintf(stderr, COLOR_RED "[ERROR]" _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
|
||||
#else
|
||||
#define LOG_ERROR(msg, ...)
|
||||
#endif
|
||||
|
||||
#if LOG_LEVEL >= LOG_LEVEL_FATAL
|
||||
#define LOG_FATAL(msg, ...) printf(COLOR_MAGENTA "[FATAL]" _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
|
||||
#define LOG_FATAL(msg, ...) fprintf(stderr, COLOR_MAGENTA "[FATAL]" _LOGGER_MESSAGE(msg), ##__VA_ARGS__)
|
||||
#else
|
||||
#define LOG_FATAL(msg, ...)
|
||||
#endif
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
/**
|
||||
* @brief Information about this controller
|
||||
*/
|
||||
typedef struct controller
|
||||
typedef struct
|
||||
{
|
||||
/**
|
||||
* @brief A unique UUID for this controller
|
||||
|
@ -22,7 +22,7 @@ typedef struct controller
|
|||
*
|
||||
* Includes a \0 terminator.
|
||||
*/
|
||||
char name[CONTROLLER_NAME_LENGTH + 1];
|
||||
char name[MAX_NAME_LENGTH + 1];
|
||||
/**
|
||||
* @brief The command port the controller was bound to
|
||||
*/
|
||||
|
@ -35,22 +35,22 @@ typedef struct controller
|
|||
* @brief Amount of relays available to this controller
|
||||
*/
|
||||
uint8_t relay_count;
|
||||
relay **relays;
|
||||
relay_t **relays;
|
||||
|
||||
} controller;
|
||||
} controller_t;
|
||||
|
||||
/**
|
||||
* @brief Key to save controller information in database
|
||||
*/
|
||||
typedef enum controller_db_key
|
||||
typedef enum
|
||||
{
|
||||
KEY_META_ID = 0,
|
||||
KEY_META_NAME = 1,
|
||||
KEY_META_COMMAND_PORT = 2,
|
||||
KEY_META_DISCOVERY_PORT = 3,
|
||||
KEY_META_RELAY_COUNT = 4,
|
||||
KEY_META_RELAYS = 5,
|
||||
} controller_db_key;
|
||||
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_RELAY_COUNT = 4,
|
||||
DB_KEY_CONTROLLER_RELAYS = 5,
|
||||
} db_key_controller_e;
|
||||
|
||||
/**
|
||||
* @brief Create a new instance of controller
|
||||
|
@ -59,7 +59,7 @@ typedef enum controller_db_key
|
|||
*
|
||||
* @return A new instance of #controller
|
||||
*/
|
||||
controller*
|
||||
controller_t*
|
||||
controller_create(void);
|
||||
|
||||
|
||||
|
@ -72,20 +72,32 @@ controller_create(void);
|
|||
*
|
||||
* @return A loaded or new instance of controller or NULL on database error
|
||||
*/
|
||||
controller*
|
||||
controller_t*
|
||||
controller_load(MDB_env *mdb_env);
|
||||
|
||||
/**
|
||||
* @brief Save a controller to the database
|
||||
*
|
||||
* @param cntrlr Instance of a controller
|
||||
* @param controller Instance of a controller
|
||||
* @param mdb_env Already created MDB_env
|
||||
*
|
||||
* @return Indicator to show success (0) or failure (!0)
|
||||
*/
|
||||
int
|
||||
controller_save(controller *cntrlr, MDB_env *mdb_env);
|
||||
controller_save(controller_t *controller, MDB_env *mdb_env);
|
||||
|
||||
/**
|
||||
* @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
|
||||
controller_set_name(controller_t *controller, char *name);
|
||||
|
||||
void
|
||||
controller_free(controller_t *controller);
|
||||
|
||||
/**
|
||||
* @brief Debug an instance of #controller
|
||||
|
@ -95,6 +107,6 @@ controller_save(controller *cntrlr, MDB_env *mdb_env);
|
|||
* @param cntrlr #controller to debug
|
||||
*/
|
||||
void
|
||||
controller_debug(controller *cntrlr);
|
||||
controller_debug(controller_t *controller);
|
||||
|
||||
#endif //CONTROLLER_CONTROLLER_H
|
||||
|
|
19
include/models/period.h
Normal file
19
include/models/period.h
Normal file
|
@ -0,0 +1,19 @@
|
|||
#ifndef CONTROLLER_PERIOD_H
|
||||
#define CONTROLLER_PERIOD_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <time.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);
|
||||
|
||||
#endif /* CONTROLLER_PERIOD_H */
|
|
@ -2,16 +2,63 @@
|
|||
#define CONTROLLER_RELAY_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <time.h>
|
||||
#include <lmdb.h>
|
||||
|
||||
#include <config.h>
|
||||
#include <models/schedule.h>
|
||||
|
||||
typedef struct relay {
|
||||
uint8_t index;
|
||||
char name[128];
|
||||
uint16_t *schedule;
|
||||
} relay;
|
||||
typedef struct
|
||||
{
|
||||
uint8_t number;
|
||||
char name[MAX_NAME_LENGTH + 1];
|
||||
schedule_t *schedule;
|
||||
} relay_t;
|
||||
|
||||
relay*
|
||||
relay_init(uint8_t index);
|
||||
/**
|
||||
* @brief Key to save relay information in database
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
DB_KEY_RELAY_NAME = 0,
|
||||
DB_KEY_RELAY_SCHEDULE_ID = 1,
|
||||
DB_KEY_RELAY_SCHEDULE_PERIODS = 2,
|
||||
} db_key_relay_e;
|
||||
|
||||
relay_t*
|
||||
relay_create(uint8_t number);
|
||||
|
||||
void
|
||||
relay_set_name(relay_t *relay, char *name);
|
||||
|
||||
/**
|
||||
* @brief Load a relay for database or create a new one
|
||||
*
|
||||
* @param mdb_env An opened MDB_env to load from
|
||||
*
|
||||
* @return A loaded or new instance of relay
|
||||
*/
|
||||
relay_t*
|
||||
relay_load(MDB_env *mdb_env, uint8_t num);
|
||||
|
||||
/**
|
||||
* @brief Save a relay to the database
|
||||
*
|
||||
* @param relay Instance of a relay
|
||||
* @param mdb_env Already created MDB_env
|
||||
*
|
||||
* @return Indicator to show success (0) or failure (!0)
|
||||
*/
|
||||
int
|
||||
relay_save(relay_t *relay, MDB_env *mdb_env);
|
||||
|
||||
int
|
||||
relay_is_active(relay_t *relay, time_t timestamp_now);
|
||||
|
||||
void
|
||||
relay_free(relay_t *relay);
|
||||
|
||||
void
|
||||
relay_debug(relay_t *relay);
|
||||
|
||||
#endif //CONTROLLER_RELAY_H
|
||||
|
|
28
include/models/schedule.h
Normal file
28
include/models/schedule.h
Normal file
|
@ -0,0 +1,28 @@
|
|||
#ifndef CONTROLLER_SCHEDULE_H
|
||||
#define CONTROLLER_SCHEDULE_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <uuid/uuid.h>
|
||||
|
||||
#include <models/period.h>
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uuid_t id;
|
||||
uint16_t length;
|
||||
period_t **periods;
|
||||
} schedule_t;
|
||||
|
||||
schedule_t*
|
||||
schedule_create(uuid_t id, uint16_t length, uint16_t *periods_blob);
|
||||
|
||||
uint16_t*
|
||||
schedule_periods_to_blob(schedule_t *schedule);
|
||||
|
||||
void
|
||||
schedule_free(schedule_t *schedule);
|
||||
|
||||
void
|
||||
schedule_debug(schedule_t *schedule);
|
||||
|
||||
#endif /* CONTROLLER_SCHEDULE_H */
|
|
@ -1,10 +1,15 @@
|
|||
#ifndef CONTROLLER_WIRING_DEBUG_H
|
||||
#define CONTROLLER_WIRING_DEBUG_H
|
||||
|
||||
#include <logger.h>
|
||||
|
||||
#ifdef WIRING_PI_DEBUG
|
||||
#define wiringPiSetup() LOG_INFO("wiringP wiringPiSetup()")
|
||||
#define wiringPiSetup() LOG_INFO("wiringPi wiringPiSetup()")
|
||||
#define wiringPiSetupSys() LOG_INFO("wiringPi wiringPiSetupSys()")
|
||||
#define pinMode(x,y) LOG_INFO("wiringPi pinMode(%d, %d)", x, y)
|
||||
#define digitalWrite(x,y) LOG_INFO("wiringPi digitalWrite(%d, %d)", x, y)
|
||||
|
||||
#define piFaceSetup(x) LOG_INFO("wiringPi piFaceSetup(%d)", x)
|
||||
#endif
|
||||
|
||||
#endif /* CONTROLLER_WIRING_DEBUG_H */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue