add: version.h
fix: refactor
This commit is contained in:
parent
532750da74
commit
f5f9be803c
36 changed files with 27 additions and 34 deletions
|
|
@ -1,11 +1,13 @@
|
||||||
cmake_minimum_required (VERSION 3.7)
|
cmake_minimum_required (VERSION 3.7)
|
||||||
project(controller)
|
project(controller
|
||||||
|
VERSION 0.1.0
|
||||||
|
LANGUAGES C)
|
||||||
|
|
||||||
add_executable(controller main.c)
|
add_executable(controller src/main.c)
|
||||||
|
|
||||||
option(WIRING_PI_DEBUG "Use WiringPi Debugging Tool (OFF)" OFF)
|
option(WIRING_PI_DEBUG "Use WiringPi Debugging Tool (OFF)" OFF)
|
||||||
|
|
||||||
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -Wpedantic -Werror -Wall -Wextra -lwiringPi -lwiringPiDev -luuid -llmdb -ffile-prefix-map=${CMAKE_SOURCE_DIR}/=/")
|
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99 -Wpedantic -Werror -Wall -Wextra -lwiringPi -lwiringPiDev -luuid -llmdb -ffile-prefix-map=${CMAKE_SOURCE_DIR}/src/=")
|
||||||
|
|
||||||
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -fprofile-arcs -ftest-coverage")
|
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -fprofile-arcs -ftest-coverage")
|
||||||
|
|
||||||
|
|
@ -14,34 +16,34 @@ if(WIRING_PI_DEBUG)
|
||||||
add_definitions("-DWIRING_PI_DEBUG")
|
add_definitions("-DWIRING_PI_DEBUG")
|
||||||
endif(WIRING_PI_DEBUG)
|
endif(WIRING_PI_DEBUG)
|
||||||
|
|
||||||
aux_source_directory(vendor VENDOR_SRC) # vendor first to put their warnings on top
|
aux_source_directory(src/ SRC_DIR)
|
||||||
aux_source_directory(. SRC_DIR)
|
aux_source_directory(src/models MODELS_SRC)
|
||||||
aux_source_directory(models MODELS_SRC)
|
aux_source_directory(src/helpers HELPERS_SRC)
|
||||||
aux_source_directory(helpers HELPERS_SRC)
|
aux_source_directory(src/handlers HANDLERS_SRC)
|
||||||
aux_source_directory(handlers HANDLERS_SRC)
|
aux_source_directory(src/drivers DRIVERS_SRC)
|
||||||
aux_source_directory(drivers DRIVERS_SRC)
|
aux_source_directory(src/runners RUNNERS_SRC)
|
||||||
aux_source_directory(runners RUNNERS_SRC)
|
|
||||||
|
|
||||||
configure_file("controller.ini" "controller.ini" COPYONLY)
|
configure_file("controller.ini" "controller.ini" COPYONLY)
|
||||||
|
configure_file("version.h.in" "version.h" @ONLY)
|
||||||
|
|
||||||
target_sources(controller PRIVATE ${VENDOR_SRC} ${SRC_DIR} ${MODELS_SRC} ${HELPERS_SRC} ${HANDLERS_SRC} ${DRIVERS_SRC} ${RUNNERS_SRC})
|
target_sources(controller PRIVATE ${SRC_DIR} ${MODELS_SRC} ${HELPERS_SRC} ${HANDLERS_SRC} ${DRIVERS_SRC} ${RUNNERS_SRC})
|
||||||
target_include_directories(controller PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
target_include_directories(controller PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
|
||||||
target_include_directories(controller PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/vendor)
|
target_include_directories(controller PRIVATE ${CMAKE_BINARY_DIR})
|
||||||
|
|
||||||
add_custom_target(run
|
add_custom_target(run
|
||||||
COMMAND ./controller start
|
COMMAND ./controller start
|
||||||
DEPENDS controller
|
DEPENDS controller
|
||||||
WORKING_DIRECTORY ${CMAKE_PROJECT_DIR}
|
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
||||||
)
|
)
|
||||||
add_custom_target(debug
|
add_custom_target(debug
|
||||||
COMMAND valgrind ./controller start
|
COMMAND valgrind ./controller start
|
||||||
DEPENDS controller
|
DEPENDS controller
|
||||||
WORKING_DIRECTORY ${CMAKE_PROJECT_DIR}
|
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
||||||
)
|
)
|
||||||
add_custom_target(debug-full
|
add_custom_target(debug-full
|
||||||
COMMAND valgrind --leak-check=full --show-leak-kinds=all ./controller start
|
COMMAND valgrind --leak-check=full --show-leak-kinds=all ./controller start
|
||||||
DEPENDS controller
|
DEPENDS controller
|
||||||
WORKING_DIRECTORY ${CMAKE_PROJECT_DIR}
|
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
|
||||||
)
|
)
|
||||||
add_custom_target(docs
|
add_custom_target(docs
|
||||||
COMMAND doxygen
|
COMMAND doxygen
|
||||||
|
|
|
||||||
130
include/argparse.h
Normal file
130
include/argparse.h
Normal file
|
|
@ -0,0 +1,130 @@
|
||||||
|
/**
|
||||||
|
* Copyright (C) 2012-2015 Yecheng Fu <cofyc.jackson at gmail dot com>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Use of this source code is governed by a MIT-style license that can be found
|
||||||
|
* in the LICENSE file.
|
||||||
|
*/
|
||||||
|
#ifndef ARGPARSE_H
|
||||||
|
#define ARGPARSE_H
|
||||||
|
|
||||||
|
/* For c++ compatibility */
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
struct argparse;
|
||||||
|
struct argparse_option;
|
||||||
|
|
||||||
|
typedef int argparse_callback (struct argparse *self,
|
||||||
|
const struct argparse_option *option);
|
||||||
|
|
||||||
|
enum argparse_flag {
|
||||||
|
ARGPARSE_STOP_AT_NON_OPTION = 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum argparse_option_type {
|
||||||
|
/* special */
|
||||||
|
ARGPARSE_OPT_END,
|
||||||
|
ARGPARSE_OPT_GROUP,
|
||||||
|
/* options with no arguments */
|
||||||
|
ARGPARSE_OPT_BOOLEAN,
|
||||||
|
ARGPARSE_OPT_BIT,
|
||||||
|
/* options with arguments (optional or required) */
|
||||||
|
ARGPARSE_OPT_INTEGER,
|
||||||
|
ARGPARSE_OPT_FLOAT,
|
||||||
|
ARGPARSE_OPT_STRING,
|
||||||
|
};
|
||||||
|
|
||||||
|
enum argparse_option_flags {
|
||||||
|
OPT_NONEG = 1, /* disable negation */
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* argparse option
|
||||||
|
*
|
||||||
|
* `type`:
|
||||||
|
* holds the type of the option, you must have an ARGPARSE_OPT_END last in your
|
||||||
|
* array.
|
||||||
|
*
|
||||||
|
* `short_name`:
|
||||||
|
* the character to use as a short option name, '\0' if none.
|
||||||
|
*
|
||||||
|
* `long_name`:
|
||||||
|
* the long option name, without the leading dash, NULL if none.
|
||||||
|
*
|
||||||
|
* `value`:
|
||||||
|
* stores pointer to the value to be filled.
|
||||||
|
*
|
||||||
|
* `help`:
|
||||||
|
* the short help message associated to what the option does.
|
||||||
|
* Must never be NULL (except for ARGPARSE_OPT_END).
|
||||||
|
*
|
||||||
|
* `callback`:
|
||||||
|
* function is called when corresponding argument is parsed.
|
||||||
|
*
|
||||||
|
* `data`:
|
||||||
|
* associated data. Callbacks can use it like they want.
|
||||||
|
*
|
||||||
|
* `flags`:
|
||||||
|
* option flags.
|
||||||
|
*/
|
||||||
|
struct argparse_option {
|
||||||
|
enum argparse_option_type type;
|
||||||
|
const char short_name;
|
||||||
|
const char *long_name;
|
||||||
|
void *value;
|
||||||
|
const char *help;
|
||||||
|
argparse_callback *callback;
|
||||||
|
intptr_t data;
|
||||||
|
int flags;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* argpparse
|
||||||
|
*/
|
||||||
|
struct argparse {
|
||||||
|
// user supplied
|
||||||
|
const struct argparse_option *options;
|
||||||
|
const char *const *usages;
|
||||||
|
int flags;
|
||||||
|
const char *description; // a description after usage
|
||||||
|
const char *epilog; // a description at the end
|
||||||
|
// internal context
|
||||||
|
int argc;
|
||||||
|
const char **argv;
|
||||||
|
const char **out;
|
||||||
|
int cpidx;
|
||||||
|
const char *optvalue; // current option value
|
||||||
|
};
|
||||||
|
|
||||||
|
// built-in callbacks
|
||||||
|
int argparse_help_cb(struct argparse *self,
|
||||||
|
const struct argparse_option *option);
|
||||||
|
|
||||||
|
// built-in option macros
|
||||||
|
#define OPT_END() { ARGPARSE_OPT_END, 0, NULL, NULL, 0, NULL, 0, 0 }
|
||||||
|
#define OPT_BOOLEAN(...) { ARGPARSE_OPT_BOOLEAN, __VA_ARGS__ }
|
||||||
|
#define OPT_BIT(...) { ARGPARSE_OPT_BIT, __VA_ARGS__ }
|
||||||
|
#define OPT_INTEGER(...) { ARGPARSE_OPT_INTEGER, __VA_ARGS__ }
|
||||||
|
#define OPT_FLOAT(...) { ARGPARSE_OPT_FLOAT, __VA_ARGS__ }
|
||||||
|
#define OPT_STRING(...) { ARGPARSE_OPT_STRING, __VA_ARGS__ }
|
||||||
|
#define OPT_GROUP(h) { ARGPARSE_OPT_GROUP, 0, NULL, NULL, h, NULL, 0, 0 }
|
||||||
|
#define OPT_HELP() OPT_BOOLEAN('h', "help", NULL, \
|
||||||
|
"show this help message and exit", \
|
||||||
|
argparse_help_cb, 0, OPT_NONEG)
|
||||||
|
|
||||||
|
int argparse_init(struct argparse *self, struct argparse_option *options,
|
||||||
|
const char *const *usages, int flags);
|
||||||
|
void argparse_describe(struct argparse *self, const char *description,
|
||||||
|
const char *epilog);
|
||||||
|
int argparse_parse(struct argparse *self, int argc, const char **argv);
|
||||||
|
void argparse_usage(struct argparse *self);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
547
include/confini.h
Normal file
547
include/confini.h
Normal file
|
|
@ -0,0 +1,547 @@
|
||||||
|
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4 -*- */
|
||||||
|
|
||||||
|
/**
|
||||||
|
|
||||||
|
@file confini.h
|
||||||
|
@brief libconfini header
|
||||||
|
@author Stefano Gioffré
|
||||||
|
@copyright GNU General Public License, version 3 or any later version
|
||||||
|
@version 1.14.0
|
||||||
|
@date 2016-2020
|
||||||
|
@see https://madmurphy.github.io/libconfini
|
||||||
|
|
||||||
|
**/
|
||||||
|
|
||||||
|
|
||||||
|
#ifndef _LIBCONFINI_HEADER_
|
||||||
|
#define _LIBCONFINI_HEADER_
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* PRIVATE (HEADER-SCOPED) MACROS */
|
||||||
|
|
||||||
|
|
||||||
|
#define __INIFORMAT_TABLE_CB_FIELDS__(NAME, OFFSET, SIZE, DEFVAL) \
|
||||||
|
unsigned char NAME:SIZE;
|
||||||
|
#define __INIFORMAT_TABLE_CB_DEFAULT__(NAME, OFFSET, SIZE, DEFVAL) DEFVAL,
|
||||||
|
#define __INIFORMAT_TABLE_CB_ZERO__(NAME, OFFSET, SIZE, DEFVAL) 0,
|
||||||
|
#define _LIBCONFINI_INIFORMAT_TYPE_ \
|
||||||
|
struct IniFormat { INIFORMAT_TABLE_AS(__INIFORMAT_TABLE_CB_FIELDS__) }
|
||||||
|
#define _LIBCONFINI_DEFAULT_FORMAT_ \
|
||||||
|
{ INIFORMAT_TABLE_AS(__INIFORMAT_TABLE_CB_DEFAULT__) }
|
||||||
|
#define _LIBCONFINI_UNIXLIKE_FORMAT_ \
|
||||||
|
{ INIFORMAT_TABLE_AS(__INIFORMAT_TABLE_CB_ZERO__) }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* PUBLIC MACROS */
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Calls a user-given macro (that accepts four arguments) for each row
|
||||||
|
of the table
|
||||||
|
**/
|
||||||
|
/*
|
||||||
|
NOTE: The following table and the order of its rows **define** (and link
|
||||||
|
together) both the #IniFormat and #IniFormatNum data types declared in this
|
||||||
|
header
|
||||||
|
*/
|
||||||
|
#define INIFORMAT_TABLE_AS(_____) /* IniFormat table *\
|
||||||
|
|
||||||
|
NAME BIT SIZE DEFAULT
|
||||||
|
*/\
|
||||||
|
_____( delimiter_symbol, 0, 7, INI_EQUALS ) \
|
||||||
|
_____( case_sensitive, 7, 1, false )/*
|
||||||
|
*/\
|
||||||
|
_____( semicolon_marker, 8, 2, INI_DISABLED_OR_COMMENT ) \
|
||||||
|
_____( hash_marker, 10, 2, INI_DISABLED_OR_COMMENT ) \
|
||||||
|
_____( section_paths, 12, 2, INI_ABSOLUTE_AND_RELATIVE ) \
|
||||||
|
_____( multiline_nodes, 14, 2, INI_MULTILINE_EVERYWHERE )/*
|
||||||
|
*/\
|
||||||
|
_____( no_single_quotes, 16, 1, false ) \
|
||||||
|
_____( no_double_quotes, 17, 1, false ) \
|
||||||
|
_____( no_spaces_in_names, 18, 1, false ) \
|
||||||
|
_____( implicit_is_not_empty, 19, 1, false ) \
|
||||||
|
_____( do_not_collapse_values, 20, 1, false ) \
|
||||||
|
_____( preserve_empty_quotes, 21, 1, false ) \
|
||||||
|
_____( disabled_after_space, 22, 1, false ) \
|
||||||
|
_____( disabled_can_be_implicit, 23, 1, false )
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Checks whether a format does **not** support escape sequences
|
||||||
|
**/
|
||||||
|
#define INIFORMAT_HAS_NO_ESC(FORMAT) \
|
||||||
|
(FORMAT.multiline_nodes == INI_NO_MULTILINE && \
|
||||||
|
FORMAT.no_double_quotes && FORMAT.no_single_quotes)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* PUBLIC TYPEDEFS */
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief 24-bit bitfield representing the format of an INI file (INI
|
||||||
|
dialect)
|
||||||
|
**/
|
||||||
|
typedef _LIBCONFINI_INIFORMAT_TYPE_ IniFormat;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Global statistics about an INI file
|
||||||
|
**/
|
||||||
|
typedef struct IniStatistics {
|
||||||
|
const IniFormat format;
|
||||||
|
const size_t bytes;
|
||||||
|
const size_t members;
|
||||||
|
} IniStatistics;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Dispatch of a single INI node
|
||||||
|
**/
|
||||||
|
typedef struct IniDispatch {
|
||||||
|
const IniFormat format;
|
||||||
|
uint8_t type;
|
||||||
|
char * data;
|
||||||
|
char * value;
|
||||||
|
const char * append_to;
|
||||||
|
size_t d_len;
|
||||||
|
size_t v_len;
|
||||||
|
size_t at_len;
|
||||||
|
size_t dispatch_id;
|
||||||
|
} IniDispatch;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief The unique ID of an INI format (24-bit maximum)
|
||||||
|
**/
|
||||||
|
typedef uint32_t IniFormatNum;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Callback function for handling an #IniStatistics structure
|
||||||
|
**/
|
||||||
|
typedef int (* IniStatsHandler) (
|
||||||
|
IniStatistics * statistics,
|
||||||
|
void * user_data
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Callback function for handling an #IniDispatch structure
|
||||||
|
**/
|
||||||
|
typedef int (* IniDispHandler) (
|
||||||
|
IniDispatch * dispatch,
|
||||||
|
void * user_data
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Callback function for handling an INI string belonging to a
|
||||||
|
sequence of INI strings
|
||||||
|
**/
|
||||||
|
typedef int (* IniStrHandler) (
|
||||||
|
char * ini_string,
|
||||||
|
size_t string_length,
|
||||||
|
size_t string_num,
|
||||||
|
IniFormat format,
|
||||||
|
void * user_data
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Callback function for handling a selected fragment of an INI string
|
||||||
|
**/
|
||||||
|
typedef int (* IniSubstrHandler) (
|
||||||
|
const char * ini_string,
|
||||||
|
size_t fragm_offset,
|
||||||
|
size_t fragm_length,
|
||||||
|
size_t fragm_num,
|
||||||
|
IniFormat format,
|
||||||
|
void * user_data
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* PUBLIC FUNCTIONS */
|
||||||
|
|
||||||
|
|
||||||
|
extern int strip_ini_cache (
|
||||||
|
register char * const ini_source,
|
||||||
|
const size_t ini_length,
|
||||||
|
const IniFormat format,
|
||||||
|
const IniStatsHandler f_init,
|
||||||
|
const IniDispHandler f_foreach,
|
||||||
|
void * const user_data
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
extern int load_ini_file (
|
||||||
|
FILE * const ini_file,
|
||||||
|
const IniFormat format,
|
||||||
|
const IniStatsHandler f_init,
|
||||||
|
const IniDispHandler f_foreach,
|
||||||
|
void * const user_data
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
extern int load_ini_path (
|
||||||
|
const char * const path,
|
||||||
|
const IniFormat format,
|
||||||
|
const IniStatsHandler f_init,
|
||||||
|
const IniDispHandler f_foreach,
|
||||||
|
void * const user_data
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
extern bool ini_string_match_ss (
|
||||||
|
const char * const simple_string_a,
|
||||||
|
const char * const simple_string_b,
|
||||||
|
const IniFormat format
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
extern bool ini_string_match_si (
|
||||||
|
const char * const simple_string,
|
||||||
|
const char * const ini_string,
|
||||||
|
const IniFormat format
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
extern bool ini_string_match_ii (
|
||||||
|
const char * const ini_string_a,
|
||||||
|
const char * const ini_string_b,
|
||||||
|
const IniFormat format
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
extern bool ini_array_match (
|
||||||
|
const char * const ini_string_a,
|
||||||
|
const char * const ini_string_b,
|
||||||
|
const char delimiter,
|
||||||
|
const IniFormat format
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
extern size_t ini_unquote (
|
||||||
|
char * const ini_string,
|
||||||
|
const IniFormat format
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
extern size_t ini_string_parse (
|
||||||
|
char * const ini_string,
|
||||||
|
const IniFormat format
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
extern size_t ini_array_get_length (
|
||||||
|
const char * const ini_string,
|
||||||
|
const char delimiter,
|
||||||
|
const IniFormat format
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
extern int ini_array_foreach (
|
||||||
|
const char * const ini_string,
|
||||||
|
const char delimiter,
|
||||||
|
const IniFormat format,
|
||||||
|
const IniSubstrHandler f_foreach,
|
||||||
|
void * const user_data
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
extern size_t ini_array_shift (
|
||||||
|
const char ** const ini_strptr,
|
||||||
|
const char delimiter,
|
||||||
|
const IniFormat format
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
extern size_t ini_array_collapse (
|
||||||
|
char * const ini_string,
|
||||||
|
const char delimiter,
|
||||||
|
const IniFormat format
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
extern char * ini_array_break (
|
||||||
|
char * const ini_string,
|
||||||
|
const char delimiter,
|
||||||
|
const IniFormat format
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
extern char * ini_array_release (
|
||||||
|
char ** const ini_strptr,
|
||||||
|
const char delimiter,
|
||||||
|
const IniFormat format
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
extern int ini_array_split (
|
||||||
|
char * const ini_string,
|
||||||
|
const char delimiter,
|
||||||
|
const IniFormat format,
|
||||||
|
const IniStrHandler f_foreach,
|
||||||
|
void * const user_data
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
extern void ini_global_set_lowercase_mode (
|
||||||
|
const bool lowercase
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
extern void ini_global_set_implicit_value (
|
||||||
|
char * const implicit_value,
|
||||||
|
const size_t implicit_v_len
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
extern IniFormatNum ini_fton (
|
||||||
|
const IniFormat format
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
extern IniFormat ini_ntof (
|
||||||
|
const IniFormatNum format_id
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
extern int ini_get_bool (
|
||||||
|
const char * const ini_string,
|
||||||
|
const int when_fail
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* PUBLIC LINKS */
|
||||||
|
|
||||||
|
|
||||||
|
extern int (* const ini_get_int) (
|
||||||
|
const char * ini_string
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
extern long int (* const ini_get_lint) (
|
||||||
|
const char * ini_string
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
extern long long int (* const ini_get_llint) (
|
||||||
|
const char * ini_string
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
extern double (* const ini_get_double) (
|
||||||
|
const char * ini_string
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Legacy support, soon to be replaced with a `float` data type --
|
||||||
|
please **do not use `ini_get_float()`!**
|
||||||
|
**/
|
||||||
|
#define ini_get_float \
|
||||||
|
_Pragma("GCC warning \"function `ini_get_float()` is deprecated for parsing a `double` data type; use `ini_get_double()` instead\"") \
|
||||||
|
ini_get_double
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* PUBLIC CONSTANTS AND VARIABLES */
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Error mask (flags not present in user-generated interruptions)
|
||||||
|
**/
|
||||||
|
#define CONFINI_ERROR 252
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Error codes
|
||||||
|
**/
|
||||||
|
enum ConfiniInterruptNo {
|
||||||
|
CONFINI_SUCCESS = 0, /**< There have been no interruptions, everything
|
||||||
|
went well [value=0] **/
|
||||||
|
CONFINI_IINTR = 1, /**< Interrupted by the user during `f_init()`
|
||||||
|
[value=1] **/
|
||||||
|
CONFINI_FEINTR = 2, /**< Interrupted by the user during `f_foreach()`
|
||||||
|
[value=2] **/
|
||||||
|
CONFINI_ENOENT = 4, /**< File inaccessible [value=4] **/
|
||||||
|
CONFINI_ENOMEM = 5, /**< Error allocating virtual memory [value=5] **/
|
||||||
|
CONFINI_EIO = 6, /**< Error reading the file [value=6] **/
|
||||||
|
CONFINI_EOOR = 7, /**< Out-of-range error: callbacks are more than
|
||||||
|
expected [value=7] **/
|
||||||
|
CONFINI_EBADF = 8, /**< The stream specified is not a seekable stream
|
||||||
|
[value=8] **/
|
||||||
|
CONFINI_EFBIG = 9, /**< File too large [value=9] **/
|
||||||
|
CONFINI_EROADDR = 10 /**< Address is read-only [value=10] **/
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief INI node types
|
||||||
|
**/
|
||||||
|
enum IniNodeType {
|
||||||
|
INI_UNKNOWN = 0, /**< This is a node impossible to categorize
|
||||||
|
[value=0] **/
|
||||||
|
INI_VALUE = 1, /**< Not used by **libconfini** (values are
|
||||||
|
dispatched together with keys) -- but
|
||||||
|
available for user's implementations
|
||||||
|
[value=1] **/
|
||||||
|
INI_KEY = 2, /**< This is a key [value=2] **/
|
||||||
|
INI_SECTION = 3, /**< This is a section or a section path
|
||||||
|
[value=3] **/
|
||||||
|
INI_COMMENT = 4, /**< This is a comment [value=4] **/
|
||||||
|
INI_INLINE_COMMENT = 5, /**< This is an inline comment [value=5] **/
|
||||||
|
INI_DISABLED_KEY = 6, /**< This is a disabled key [value=6] **/
|
||||||
|
INI_DISABLED_SECTION = 7 /**< This is a disabled section path
|
||||||
|
[value=7] **/
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Common array and key-value delimiters (but a delimiter may also be
|
||||||
|
any other ASCII character not present in this list)
|
||||||
|
**/
|
||||||
|
enum IniDelimiters {
|
||||||
|
INI_ANY_SPACE = 0, /**< In multi-line INIs:
|
||||||
|
`/(?:\\(?:\n\r?|\r\n?)|[\t \v\f])+/`, in
|
||||||
|
non-multi-line INIs: `/[\t \v\f])+/` **/
|
||||||
|
INI_EQUALS = '=', /**< Equals character (`=`) **/
|
||||||
|
INI_COLON = ':', /**< Colon character (`:`) **/
|
||||||
|
INI_DOT = '.', /**< Dot character (`.`) **/
|
||||||
|
INI_COMMA = ',' /**< Comma character (`,`) **/
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Possible values of #IniFormat::semicolon_marker and
|
||||||
|
#IniFormat::hash_marker (i.e., meaning of `/\s+;/` and `/\s+#/` in
|
||||||
|
respect to a format)
|
||||||
|
**/
|
||||||
|
enum IniCommentMarker {
|
||||||
|
INI_DISABLED_OR_COMMENT = 0, /**< This marker opens a comment or a
|
||||||
|
disabled entry **/
|
||||||
|
INI_ONLY_COMMENT = 1, /**< This marker opens a comment **/
|
||||||
|
INI_IGNORE = 2, /**< This marker opens a comment that has
|
||||||
|
been marked for deletion and must not
|
||||||
|
be dispatched or counted **/
|
||||||
|
INI_IS_NOT_A_MARKER = 3 /**< This is not a marker at all, but a
|
||||||
|
normal character instead **/
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Possible values of #IniFormat::section_paths
|
||||||
|
**/
|
||||||
|
enum IniSectionPaths {
|
||||||
|
INI_ABSOLUTE_AND_RELATIVE = 0, /**< Section paths starting with a dot
|
||||||
|
express nesting to the current parent,
|
||||||
|
to root otherwise **/
|
||||||
|
INI_ABSOLUTE_ONLY = 1, /**< Section paths starting with a dot will
|
||||||
|
be cleaned of their leading dot and
|
||||||
|
appended to root **/
|
||||||
|
INI_ONE_LEVEL_ONLY = 2, /**< Format supports sections, but the dot
|
||||||
|
does not express nesting and is not a
|
||||||
|
meta-character **/
|
||||||
|
INI_NO_SECTIONS = 3 /**< Format does *not* support sections --
|
||||||
|
`/\[[^\]]*\]/g`, if any, will be
|
||||||
|
treated as keys! **/
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Possible values of #IniFormat::multiline_nodes
|
||||||
|
**/
|
||||||
|
enum IniMultiline {
|
||||||
|
INI_MULTILINE_EVERYWHERE = 0, /**< Comments, section paths and keys
|
||||||
|
-- disabled or not -- are allowed
|
||||||
|
to be multi-line **/
|
||||||
|
INI_BUT_COMMENTS = 1, /**< Only section paths and keys --
|
||||||
|
disabled or not -- are allowed to
|
||||||
|
be multi-line **/
|
||||||
|
INI_BUT_DISABLED_AND_COMMENTS = 2, /**< Only active section paths and
|
||||||
|
active keys are allowed to be
|
||||||
|
multi-line **/
|
||||||
|
INI_NO_MULTILINE = 3 /**< Multi-line escape sequences are
|
||||||
|
disabled **/
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief A model format for standard INI files
|
||||||
|
**/
|
||||||
|
static const IniFormat INI_DEFAULT_FORMAT = _LIBCONFINI_DEFAULT_FORMAT_;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief A model format for Unix-like .conf files (where space characters
|
||||||
|
are delimiters between keys and values)
|
||||||
|
**/
|
||||||
|
/* All fields are set to `0` here. */
|
||||||
|
static const IniFormat INI_UNIXLIKE_FORMAT = _LIBCONFINI_UNIXLIKE_FORMAT_;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief If set to `true`, key and section names in case-insensitive INI
|
||||||
|
formats will be dispatched lowercase, verbatim otherwise (default
|
||||||
|
value: `false`)
|
||||||
|
**/
|
||||||
|
extern bool INI_GLOBAL_LOWERCASE_MODE;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Value to be assigned to implicit keys (default value: `NULL`)
|
||||||
|
**/
|
||||||
|
extern char * INI_GLOBAL_IMPLICIT_VALUE;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Length of the value assigned to implicit keys (default value: `0`)
|
||||||
|
**/
|
||||||
|
extern size_t INI_GLOBAL_IMPLICIT_V_LEN;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* CLEAN THE PRIVATE ENVIRONMENT */
|
||||||
|
|
||||||
|
|
||||||
|
#undef _LIBCONFINI_UNIXLIKE_FORMAT_
|
||||||
|
#undef _LIBCONFINI_DEFAULT_FORMAT_
|
||||||
|
#undef _LIBCONFINI_INIFORMAT_TYPE_
|
||||||
|
#undef __INIFORMAT_TABLE_CB_ZERO__
|
||||||
|
#undef __INIFORMAT_TABLE_CB_DEFAULT__
|
||||||
|
#undef __INIFORMAT_TABLE_CB_FIELDS__
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* END OF `_LIBCONFINI_HEADER_` */
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* EOF */
|
||||||
|
|
||||||
7151
include/mpack.h
Normal file
7151
include/mpack.h
Normal file
File diff suppressed because it is too large
Load diff
384
src/argparse.c
Normal file
384
src/argparse.c
Normal file
|
|
@ -0,0 +1,384 @@
|
||||||
|
/**
|
||||||
|
* Copyright (C) 2012-2015 Yecheng Fu <cofyc.jackson at gmail dot com>
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Use of this source code is governed by a MIT-style license that can be found
|
||||||
|
* in the LICENSE file.
|
||||||
|
*/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include "argparse.h"
|
||||||
|
|
||||||
|
#define OPT_UNSET 1
|
||||||
|
#define OPT_LONG (1 << 1)
|
||||||
|
|
||||||
|
static const char *
|
||||||
|
prefix_skip(const char *str, const char *prefix)
|
||||||
|
{
|
||||||
|
size_t len = strlen(prefix);
|
||||||
|
return strncmp(str, prefix, len) ? NULL : str + len;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
prefix_cmp(const char *str, const char *prefix)
|
||||||
|
{
|
||||||
|
for (;; str++, prefix++)
|
||||||
|
if (!*prefix) {
|
||||||
|
return 0;
|
||||||
|
} else if (*str != *prefix) {
|
||||||
|
return (unsigned char)*prefix - (unsigned char)*str;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
argparse_error(struct argparse *self, const struct argparse_option *opt,
|
||||||
|
const char *reason, int flags)
|
||||||
|
{
|
||||||
|
(void)self;
|
||||||
|
if (flags & OPT_LONG) {
|
||||||
|
fprintf(stderr, "error: option `--%s` %s\n", opt->long_name, reason);
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "error: option `-%c` %s\n", opt->short_name, reason);
|
||||||
|
}
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
argparse_getvalue(struct argparse *self, const struct argparse_option *opt,
|
||||||
|
int flags)
|
||||||
|
{
|
||||||
|
const char *s = NULL;
|
||||||
|
if (!opt->value)
|
||||||
|
goto skipped;
|
||||||
|
switch (opt->type) {
|
||||||
|
case ARGPARSE_OPT_BOOLEAN:
|
||||||
|
if (flags & OPT_UNSET) {
|
||||||
|
*(int *)opt->value = *(int *)opt->value - 1;
|
||||||
|
} else {
|
||||||
|
*(int *)opt->value = *(int *)opt->value + 1;
|
||||||
|
}
|
||||||
|
if (*(int *)opt->value < 0) {
|
||||||
|
*(int *)opt->value = 0;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ARGPARSE_OPT_BIT:
|
||||||
|
if (flags & OPT_UNSET) {
|
||||||
|
*(int *)opt->value &= ~opt->data;
|
||||||
|
} else {
|
||||||
|
*(int *)opt->value |= opt->data;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ARGPARSE_OPT_STRING:
|
||||||
|
if (self->optvalue) {
|
||||||
|
*(const char **)opt->value = self->optvalue;
|
||||||
|
self->optvalue = NULL;
|
||||||
|
} else if (self->argc > 1) {
|
||||||
|
self->argc--;
|
||||||
|
*(const char **)opt->value = *++self->argv;
|
||||||
|
} else {
|
||||||
|
argparse_error(self, opt, "requires a value", flags);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ARGPARSE_OPT_INTEGER:
|
||||||
|
errno = 0;
|
||||||
|
if (self->optvalue) {
|
||||||
|
*(int *)opt->value = strtol(self->optvalue, (char **)&s, 0);
|
||||||
|
self->optvalue = NULL;
|
||||||
|
} else if (self->argc > 1) {
|
||||||
|
self->argc--;
|
||||||
|
*(int *)opt->value = strtol(*++self->argv, (char **)&s, 0);
|
||||||
|
} else {
|
||||||
|
argparse_error(self, opt, "requires a value", flags);
|
||||||
|
}
|
||||||
|
if (errno)
|
||||||
|
argparse_error(self, opt, strerror(errno), flags);
|
||||||
|
if (s[0] != '\0')
|
||||||
|
argparse_error(self, opt, "expects an integer value", flags);
|
||||||
|
break;
|
||||||
|
case ARGPARSE_OPT_FLOAT:
|
||||||
|
errno = 0;
|
||||||
|
if (self->optvalue) {
|
||||||
|
*(float *)opt->value = strtof(self->optvalue, (char **)&s);
|
||||||
|
self->optvalue = NULL;
|
||||||
|
} else if (self->argc > 1) {
|
||||||
|
self->argc--;
|
||||||
|
*(float *)opt->value = strtof(*++self->argv, (char **)&s);
|
||||||
|
} else {
|
||||||
|
argparse_error(self, opt, "requires a value", flags);
|
||||||
|
}
|
||||||
|
if (errno)
|
||||||
|
argparse_error(self, opt, strerror(errno), flags);
|
||||||
|
if (s[0] != '\0')
|
||||||
|
argparse_error(self, opt, "expects a numerical value", flags);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
assert(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
skipped:
|
||||||
|
if (opt->callback) {
|
||||||
|
return opt->callback(self, opt);
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
argparse_options_check(const struct argparse_option *options)
|
||||||
|
{
|
||||||
|
for (; options->type != ARGPARSE_OPT_END; options++) {
|
||||||
|
switch (options->type) {
|
||||||
|
case ARGPARSE_OPT_END:
|
||||||
|
case ARGPARSE_OPT_BOOLEAN:
|
||||||
|
case ARGPARSE_OPT_BIT:
|
||||||
|
case ARGPARSE_OPT_INTEGER:
|
||||||
|
case ARGPARSE_OPT_FLOAT:
|
||||||
|
case ARGPARSE_OPT_STRING:
|
||||||
|
case ARGPARSE_OPT_GROUP:
|
||||||
|
continue;
|
||||||
|
default:
|
||||||
|
fprintf(stderr, "wrong option type: %d", options->type);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
argparse_short_opt(struct argparse *self, const struct argparse_option *options)
|
||||||
|
{
|
||||||
|
for (; options->type != ARGPARSE_OPT_END; options++) {
|
||||||
|
if (options->short_name == *self->optvalue) {
|
||||||
|
self->optvalue = self->optvalue[1] ? self->optvalue + 1 : NULL;
|
||||||
|
return argparse_getvalue(self, options, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
argparse_long_opt(struct argparse *self, const struct argparse_option *options)
|
||||||
|
{
|
||||||
|
for (; options->type != ARGPARSE_OPT_END; options++) {
|
||||||
|
const char *rest;
|
||||||
|
int opt_flags = 0;
|
||||||
|
if (!options->long_name)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
rest = prefix_skip(self->argv[0] + 2, options->long_name);
|
||||||
|
if (!rest) {
|
||||||
|
// negation disabled?
|
||||||
|
if (options->flags & OPT_NONEG) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// only OPT_BOOLEAN/OPT_BIT supports negation
|
||||||
|
if (options->type != ARGPARSE_OPT_BOOLEAN && options->type !=
|
||||||
|
ARGPARSE_OPT_BIT) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (prefix_cmp(self->argv[0] + 2, "no-")) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
rest = prefix_skip(self->argv[0] + 2 + 3, options->long_name);
|
||||||
|
if (!rest)
|
||||||
|
continue;
|
||||||
|
opt_flags |= OPT_UNSET;
|
||||||
|
}
|
||||||
|
if (*rest) {
|
||||||
|
if (*rest != '=')
|
||||||
|
continue;
|
||||||
|
self->optvalue = rest + 1;
|
||||||
|
}
|
||||||
|
return argparse_getvalue(self, options, opt_flags | OPT_LONG);
|
||||||
|
}
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
argparse_init(struct argparse *self, struct argparse_option *options,
|
||||||
|
const char *const *usages, int flags)
|
||||||
|
{
|
||||||
|
memset(self, 0, sizeof(*self));
|
||||||
|
self->options = options;
|
||||||
|
self->usages = usages;
|
||||||
|
self->flags = flags;
|
||||||
|
self->description = NULL;
|
||||||
|
self->epilog = NULL;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
argparse_describe(struct argparse *self, const char *description,
|
||||||
|
const char *epilog)
|
||||||
|
{
|
||||||
|
self->description = description;
|
||||||
|
self->epilog = epilog;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
argparse_parse(struct argparse *self, int argc, const char **argv)
|
||||||
|
{
|
||||||
|
self->argc = argc - 1;
|
||||||
|
self->argv = argv + 1;
|
||||||
|
self->out = argv;
|
||||||
|
|
||||||
|
argparse_options_check(self->options);
|
||||||
|
|
||||||
|
for (; self->argc; self->argc--, self->argv++) {
|
||||||
|
const char *arg = self->argv[0];
|
||||||
|
if (arg[0] != '-' || !arg[1]) {
|
||||||
|
if (self->flags & ARGPARSE_STOP_AT_NON_OPTION) {
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
// if it's not option or is a single char '-', copy verbatim
|
||||||
|
self->out[self->cpidx++] = self->argv[0];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// short option
|
||||||
|
if (arg[1] != '-') {
|
||||||
|
self->optvalue = arg + 1;
|
||||||
|
switch (argparse_short_opt(self, self->options)) {
|
||||||
|
case -1:
|
||||||
|
break;
|
||||||
|
case -2:
|
||||||
|
goto unknown;
|
||||||
|
}
|
||||||
|
while (self->optvalue) {
|
||||||
|
switch (argparse_short_opt(self, self->options)) {
|
||||||
|
case -1:
|
||||||
|
break;
|
||||||
|
case -2:
|
||||||
|
goto unknown;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// if '--' presents
|
||||||
|
if (!arg[2]) {
|
||||||
|
self->argc--;
|
||||||
|
self->argv++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
// long option
|
||||||
|
switch (argparse_long_opt(self, self->options)) {
|
||||||
|
case -1:
|
||||||
|
break;
|
||||||
|
case -2:
|
||||||
|
goto unknown;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
|
||||||
|
unknown:
|
||||||
|
fprintf(stderr, "error: unknown option `%s`\n", self->argv[0]);
|
||||||
|
argparse_usage(self);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
end:
|
||||||
|
memmove(self->out + self->cpidx, self->argv,
|
||||||
|
self->argc * sizeof(*self->out));
|
||||||
|
self->out[self->cpidx + self->argc] = NULL;
|
||||||
|
|
||||||
|
return self->cpidx + self->argc;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
argparse_usage(struct argparse *self)
|
||||||
|
{
|
||||||
|
if (self->usages) {
|
||||||
|
fprintf(stdout, "Usage: %s\n", *self->usages++);
|
||||||
|
while (*self->usages && **self->usages)
|
||||||
|
fprintf(stdout, " or: %s\n", *self->usages++);
|
||||||
|
} else {
|
||||||
|
fprintf(stdout, "Usage:\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
// print description
|
||||||
|
if (self->description)
|
||||||
|
fprintf(stdout, "%s\n", self->description);
|
||||||
|
|
||||||
|
fputc('\n', stdout);
|
||||||
|
|
||||||
|
const struct argparse_option *options;
|
||||||
|
|
||||||
|
// figure out best width
|
||||||
|
size_t usage_opts_width = 0;
|
||||||
|
size_t len;
|
||||||
|
options = self->options;
|
||||||
|
for (; options->type != ARGPARSE_OPT_END; options++) {
|
||||||
|
len = 0;
|
||||||
|
if ((options)->short_name) {
|
||||||
|
len += 2;
|
||||||
|
}
|
||||||
|
if ((options)->short_name && (options)->long_name) {
|
||||||
|
len += 2; // separator ", "
|
||||||
|
}
|
||||||
|
if ((options)->long_name) {
|
||||||
|
len += strlen((options)->long_name) + 2;
|
||||||
|
}
|
||||||
|
if (options->type == ARGPARSE_OPT_INTEGER) {
|
||||||
|
len += strlen("=<int>");
|
||||||
|
}
|
||||||
|
if (options->type == ARGPARSE_OPT_FLOAT) {
|
||||||
|
len += strlen("=<flt>");
|
||||||
|
} else if (options->type == ARGPARSE_OPT_STRING) {
|
||||||
|
len += strlen("=<str>");
|
||||||
|
}
|
||||||
|
len = (len + 3) - ((len + 3) & 3);
|
||||||
|
if (usage_opts_width < len) {
|
||||||
|
usage_opts_width = len;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
usage_opts_width += 4; // 4 spaces prefix
|
||||||
|
|
||||||
|
options = self->options;
|
||||||
|
for (; options->type != ARGPARSE_OPT_END; options++) {
|
||||||
|
size_t pos = 0;
|
||||||
|
int pad = 0;
|
||||||
|
if (options->type == ARGPARSE_OPT_GROUP) {
|
||||||
|
fputc('\n', stdout);
|
||||||
|
fprintf(stdout, "%s", options->help);
|
||||||
|
fputc('\n', stdout);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
pos = fprintf(stdout, " ");
|
||||||
|
if (options->short_name) {
|
||||||
|
pos += fprintf(stdout, "-%c", options->short_name);
|
||||||
|
}
|
||||||
|
if (options->long_name && options->short_name) {
|
||||||
|
pos += fprintf(stdout, ", ");
|
||||||
|
}
|
||||||
|
if (options->long_name) {
|
||||||
|
pos += fprintf(stdout, "--%s", options->long_name);
|
||||||
|
}
|
||||||
|
if (options->type == ARGPARSE_OPT_INTEGER) {
|
||||||
|
pos += fprintf(stdout, "=<int>");
|
||||||
|
} else if (options->type == ARGPARSE_OPT_FLOAT) {
|
||||||
|
pos += fprintf(stdout, "=<flt>");
|
||||||
|
} else if (options->type == ARGPARSE_OPT_STRING) {
|
||||||
|
pos += fprintf(stdout, "=<str>");
|
||||||
|
}
|
||||||
|
if (pos <= usage_opts_width) {
|
||||||
|
pad = usage_opts_width - pos;
|
||||||
|
} else {
|
||||||
|
fputc('\n', stdout);
|
||||||
|
pad = usage_opts_width;
|
||||||
|
}
|
||||||
|
fprintf(stdout, "%*s%s\n", pad + 2, "", options->help);
|
||||||
|
}
|
||||||
|
|
||||||
|
// print epilog
|
||||||
|
if (self->epilog)
|
||||||
|
fprintf(stdout, "%s\n", self->epilog);
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
argparse_help_cb(struct argparse *self, const struct argparse_option *option)
|
||||||
|
{
|
||||||
|
(void)option;
|
||||||
|
argparse_usage(self);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
5016
src/confini.c
Normal file
5016
src/confini.c
Normal file
File diff suppressed because it is too large
Load diff
31
src/database.c
Normal file
31
src/database.c
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <lmdb.h>
|
||||||
|
|
||||||
|
#include <config.h>
|
||||||
|
#include <constants.h>
|
||||||
|
|
||||||
|
void
|
||||||
|
database_setup(MDB_env **mdb_env, config_t *config)
|
||||||
|
{
|
||||||
|
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, config->database, MDB_NOSUBDIR, 0700) != 0)
|
||||||
|
{
|
||||||
|
perror("Can't open mdb file");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
14
src/drivers/gpio.c
Normal file
14
src/drivers/gpio.c
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
#include <wiringPi.h>
|
||||||
|
#include <piFace.h>
|
||||||
|
#include <wiring_debug.h>
|
||||||
|
|
||||||
|
#include <drivers.h>
|
||||||
|
|
||||||
|
void
|
||||||
|
driver_gpio_set(int pin, int value)
|
||||||
|
{
|
||||||
|
// disable "unused parameter" warning (happens when using wiring_debug)
|
||||||
|
(void)pin;
|
||||||
|
(void)value;
|
||||||
|
digitalWrite(pin, value);
|
||||||
|
}
|
||||||
14
src/drivers/piface.c
Normal file
14
src/drivers/piface.c
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
#include <wiringPi.h>
|
||||||
|
#include <piFace.h>
|
||||||
|
#include <wiring_debug.h>
|
||||||
|
|
||||||
|
#include <drivers.h>
|
||||||
|
|
||||||
|
void
|
||||||
|
driver_piface_set(int pin, int value)
|
||||||
|
{
|
||||||
|
// disable "unused parameter" warning (happens when using wiring_debug)
|
||||||
|
(void)pin;
|
||||||
|
(void)value;
|
||||||
|
digitalWrite(PIFACE_GPIO_BASE + pin, value);
|
||||||
|
}
|
||||||
142
src/handlers/command.c
Normal file
142
src/handlers/command.c
Normal file
|
|
@ -0,0 +1,142 @@
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <uuid/uuid.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include <logger.h>
|
||||||
|
#include <handlers.h>
|
||||||
|
#include <helpers.h>
|
||||||
|
#include <enums.h>
|
||||||
|
#include <mpack.h>
|
||||||
|
#include <models/schedule.h>
|
||||||
|
|
||||||
|
static void
|
||||||
|
handler_command_set_name(mpack_node_t map, controller_t *controller)
|
||||||
|
{
|
||||||
|
char name_buffer[MAX_NAME_LENGTH + 1];
|
||||||
|
mpack_node_copy_cstr(mpack_node_map_uint(map, COMMAND_MAPPING_NAME), name_buffer, MAX_NAME_LENGTH + 1);
|
||||||
|
controller_set_name(controller, name_buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
handler_command_set_schedule(mpack_node_t map, controller_t *controller)
|
||||||
|
{
|
||||||
|
uint8_t relay_num = mpack_node_u8(mpack_node_map_uint(map, COMMAND_MAPPING_RELAY_NUM));
|
||||||
|
|
||||||
|
relay_t *target_relay = controller->relays[relay_num];
|
||||||
|
|
||||||
|
mpack_node_t schedules_array = mpack_node_map_uint(map, COMMAND_MAPPING_SCHEDULES_ARRAY);
|
||||||
|
|
||||||
|
for(int i = 0; i < 7; ++i)
|
||||||
|
{
|
||||||
|
mpack_node_t schedules_map = mpack_node_array_at(schedules_array, i);
|
||||||
|
|
||||||
|
uuid_t schedule_id;
|
||||||
|
memcpy(schedule_id, mpack_node_data(mpack_node_map_uint(schedules_map, COMMAND_MAPPING_SCHEDULE_ID)), sizeof(uuid_t));
|
||||||
|
|
||||||
|
uint16_t periods_count = mpack_node_u16(mpack_node_map_uint(schedules_map, COMMAND_MAPPING_PERIODS_COUNT));
|
||||||
|
uint16_t *periods = (uint16_t*)mpack_node_bin_data(mpack_node_map_uint(schedules_map, COMMAND_MAPPING_PERIODS_BLOB));
|
||||||
|
|
||||||
|
if(target_relay->schedules[i])
|
||||||
|
{
|
||||||
|
schedule_free(target_relay->schedules[i]);
|
||||||
|
}
|
||||||
|
target_relay->schedules[i] = schedule_create(schedule_id, i, periods_count, periods);
|
||||||
|
}
|
||||||
|
|
||||||
|
relay_debug(target_relay);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
handler_command_set_relay_name(mpack_node_t map, controller_t *controller)
|
||||||
|
{
|
||||||
|
uint8_t relay_num = mpack_node_u8(mpack_node_map_uint(map, COMMAND_MAPPING_RELAY_NUM));
|
||||||
|
const char *relay_name = mpack_node_str(mpack_node_map_uint(map, COMMAND_MAPPING_NAME));
|
||||||
|
|
||||||
|
if(relay_num < controller->relay_count)
|
||||||
|
{
|
||||||
|
relay_set_name(controller->relays[relay_num], relay_name);
|
||||||
|
}
|
||||||
|
relay_debug(controller->relays[relay_num]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
handler_command(int fd, controller_t *controller)
|
||||||
|
{
|
||||||
|
struct sockaddr_storage their_addr;
|
||||||
|
socklen_t addr_size;
|
||||||
|
int client_fd;
|
||||||
|
|
||||||
|
addr_size = sizeof(their_addr);
|
||||||
|
|
||||||
|
if((client_fd = accept(fd, (struct sockaddr *) &their_addr, &addr_size)) < 0)
|
||||||
|
{
|
||||||
|
LOG_ERROR("could not accept client: %s\n", strerror(errno));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t payload_length;
|
||||||
|
|
||||||
|
if(recv(client_fd, &payload_length, sizeof(payload_length), 0) <= 0)
|
||||||
|
{
|
||||||
|
LOG_ERROR("unable to receive header: %s\n", strerror(errno));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *payload = malloc((payload_length + 1));
|
||||||
|
ssize_t bytes_transferred;
|
||||||
|
|
||||||
|
if((bytes_transferred = recv(client_fd, payload, payload_length, 0)) <= 0)
|
||||||
|
{
|
||||||
|
LOG_ERROR("unable to receive payload: %s\n", strerror(errno));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
mpack_tree_t tree;
|
||||||
|
mpack_tree_init_data(&tree, payload, payload_length);
|
||||||
|
mpack_tree_parse(&tree);
|
||||||
|
mpack_node_t root = mpack_tree_root(&tree);
|
||||||
|
|
||||||
|
uint8_t command_code = mpack_node_u8(mpack_node_map_uint(root, COMMAND_MAPPING_CODE));
|
||||||
|
|
||||||
|
LOG_INFO("received command %d\n", command_code);
|
||||||
|
|
||||||
|
switch(command_code)
|
||||||
|
{
|
||||||
|
case COMMAND_CODE_GET_TIME:
|
||||||
|
break;
|
||||||
|
case COMMAND_CODE_GET_ID:
|
||||||
|
break;
|
||||||
|
case COMMAND_CODE_SET_NAME:
|
||||||
|
handler_command_set_name(root, controller);
|
||||||
|
break;
|
||||||
|
case COMMAND_CODE_GET_NAME:
|
||||||
|
break;
|
||||||
|
case COMMAND_CODE_SET_SCHEDULE:
|
||||||
|
handler_command_set_schedule(root, controller);
|
||||||
|
break;
|
||||||
|
case COMMAND_CODE_GET_SCHEDULE:
|
||||||
|
break;
|
||||||
|
case COMMAND_CODE_SET_RELAY_NAME:
|
||||||
|
handler_command_set_relay_name(root, controller);
|
||||||
|
break;
|
||||||
|
case COMMAND_CODE_GET_RELAY_NAME:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
LOG_ERROR("received invalid command\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(mpack_tree_destroy(&tree) != mpack_ok)
|
||||||
|
{
|
||||||
|
LOG_WARN("error when destroying mpack tree\n");
|
||||||
|
}
|
||||||
|
free(payload);
|
||||||
|
close(client_fd);
|
||||||
|
}
|
||||||
89
src/handlers/discovery.c
Normal file
89
src/handlers/discovery.c
Normal file
|
|
@ -0,0 +1,89 @@
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <uuid/uuid.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include <logger.h>
|
||||||
|
#include <handlers.h>
|
||||||
|
#include <helpers.h>
|
||||||
|
#include <mpack.h>
|
||||||
|
#include <enums.h>
|
||||||
|
|
||||||
|
void
|
||||||
|
handler_discovery(int fd, controller_t *controller)
|
||||||
|
{
|
||||||
|
ssize_t bytes_transferred;
|
||||||
|
uint16_t discovery_answer_port;
|
||||||
|
struct sockaddr_in si_other;
|
||||||
|
socklen_t slen = sizeof(si_other);
|
||||||
|
|
||||||
|
if((bytes_transferred = recvfrom(fd, &discovery_answer_port, sizeof(discovery_answer_port), 0, (struct sockaddr *) &si_other, &slen)) <= 0)
|
||||||
|
{
|
||||||
|
LOG_ERROR("received invalid discovery from %s\n", inet_ntoa(si_other.sin_addr));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
LOG_INFO("received discovery from %s:%d\n", inet_ntoa(si_other.sin_addr), discovery_answer_port);
|
||||||
|
|
||||||
|
if(discovery_answer_port == 0)
|
||||||
|
{
|
||||||
|
LOG_ERROR("invalid port received\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
char* payload;
|
||||||
|
size_t payload_size;
|
||||||
|
mpack_writer_t writer;
|
||||||
|
mpack_writer_init_growable(&writer, &payload, &payload_size);
|
||||||
|
|
||||||
|
mpack_start_map(&writer, 4);
|
||||||
|
mpack_write_uint(&writer, DISCOVERY_MAPPING_ID);
|
||||||
|
mpack_write_bin(&writer, (char*)controller->id, sizeof(uuid_t));
|
||||||
|
mpack_write_uint(&writer, DISCOVERY_MAPPING_COMMAND_PORT);
|
||||||
|
mpack_write_u16(&writer, controller->command_port);
|
||||||
|
mpack_write_uint(&writer, DISCOVERY_MAPPING_RELAY_COUNT);
|
||||||
|
mpack_write_u8(&writer, controller->relay_count);
|
||||||
|
mpack_write_uint(&writer, DISCOVERY_MAPPING_NAME);
|
||||||
|
mpack_write_cstr(&writer, controller->name);
|
||||||
|
mpack_finish_map(&writer);
|
||||||
|
|
||||||
|
// finish writing
|
||||||
|
if(mpack_writer_destroy(&writer) != mpack_ok)
|
||||||
|
{
|
||||||
|
LOG_ERROR("error writing discovery answer payload\n");
|
||||||
|
return;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int fd_answer = helper_connect_tcp_server(inet_ntoa(si_other.sin_addr), discovery_answer_port);
|
||||||
|
if(fd_answer == -1)
|
||||||
|
{
|
||||||
|
LOG_ERROR("error during connecting\n");
|
||||||
|
free(payload);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if((bytes_transferred = send(fd_answer, &payload_size, sizeof(payload_size), 0)) <= 0)
|
||||||
|
{
|
||||||
|
LOG_ERROR("error during sending\n");
|
||||||
|
free(payload);
|
||||||
|
close(fd_answer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if((bytes_transferred = send(fd_answer, payload, payload_size, 0)) <= 0)
|
||||||
|
{
|
||||||
|
LOG_ERROR("error during sending\n");
|
||||||
|
free(payload);
|
||||||
|
close(fd_answer);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
free(payload);
|
||||||
|
close(fd_answer);
|
||||||
|
}
|
||||||
46
src/handlers/loop.c
Normal file
46
src/handlers/loop.c
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#include <logger.h>
|
||||||
|
#include <models/controller.h>
|
||||||
|
#include <handlers.h>
|
||||||
|
#include <drivers.h>
|
||||||
|
#include <enums.h>
|
||||||
|
#include <helpers.h>
|
||||||
|
#include <wiringPi.h>
|
||||||
|
#include <wiring_debug.h>
|
||||||
|
|
||||||
|
void
|
||||||
|
handler_loop(controller_t *controller)
|
||||||
|
{
|
||||||
|
time_t timestamp = time(NULL);
|
||||||
|
struct tm *time_struct = localtime(×tamp);
|
||||||
|
LOG_DEBUG("===== IDLE LOOP START =====\n");
|
||||||
|
for(uint_fast8_t i = 0; i < controller->relay_count; ++i)
|
||||||
|
{
|
||||||
|
relay_t *relay = controller->relays[i];
|
||||||
|
int is_active = 0;
|
||||||
|
if(relay_is_active(relay, time_struct))
|
||||||
|
{
|
||||||
|
LOG_DEBUG("relay %d is active\n", i);
|
||||||
|
is_active = 1;
|
||||||
|
}
|
||||||
|
if(global_config.relay_configs[i].inverted)
|
||||||
|
{
|
||||||
|
is_active = !is_active;
|
||||||
|
}
|
||||||
|
switch(global_config.relay_configs[i].driver)
|
||||||
|
{
|
||||||
|
case RELAY_DRIVER_GPIO:
|
||||||
|
driver_gpio_set(global_config.relay_configs[i].pin, is_active);
|
||||||
|
break;
|
||||||
|
case RELAY_DRIVER_PIFACE:
|
||||||
|
driver_piface_set(global_config.relay_configs[i].pin, is_active);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
LOG_WARN("relay %d is not using a driver\n", i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
50
src/handlers/poll.c
Normal file
50
src/handlers/poll.c
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <lmdb.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <poll.h>
|
||||||
|
#include <signal.h>
|
||||||
|
|
||||||
|
#include <logger.h>
|
||||||
|
#include <models/controller.h>
|
||||||
|
#include <database.h>
|
||||||
|
#include <config.h>
|
||||||
|
#include <constants.h>
|
||||||
|
#include <handlers.h>
|
||||||
|
#include <drivers.h>
|
||||||
|
#include <enums.h>
|
||||||
|
#include <helpers.h>
|
||||||
|
#include <wiringPi.h>
|
||||||
|
#include <piFace.h>
|
||||||
|
#include <wiring_debug.h>
|
||||||
|
|
||||||
|
void
|
||||||
|
handler_poll(struct pollfd *fds, controller_t *controller, MDB_env *mdb_env)
|
||||||
|
{
|
||||||
|
/* An event on one of the fds has occurred. */
|
||||||
|
for(int i = 0; i < POLL_FDS_COUNT; i++) {
|
||||||
|
if(fds[i].revents & POLLIN)
|
||||||
|
{
|
||||||
|
/* data may be read on device number i. */
|
||||||
|
LOG_DEBUG("fd %i may read data\n", fds[i].fd);
|
||||||
|
switch(i)
|
||||||
|
{
|
||||||
|
case POLL_FDS_DISCOVERY:
|
||||||
|
handler_discovery(fds[i].fd, controller);
|
||||||
|
break;
|
||||||
|
case POLL_FDS_COMMAND:
|
||||||
|
handler_command(fds[i].fd, controller);
|
||||||
|
controller_save(controller, mdb_env);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(fds[i].revents & POLLHUP)
|
||||||
|
{
|
||||||
|
/* A hangup has occurred on device number i. */
|
||||||
|
LOG_DEBUG("fd %i got closed\n", fds[i].fd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
52
src/helpers/bind_server.c
Normal file
52
src/helpers/bind_server.c
Normal file
|
|
@ -0,0 +1,52 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include <logger.h>
|
||||||
|
#include <helpers.h>
|
||||||
|
|
||||||
|
int
|
||||||
|
helper_bind_tcp_server(char* addr, uint16_t port, int max_client_backlog)
|
||||||
|
{
|
||||||
|
char port_str[6];
|
||||||
|
sprintf(port_str, "%d", port);
|
||||||
|
|
||||||
|
struct addrinfo hints, *res;
|
||||||
|
int fd;
|
||||||
|
int status;
|
||||||
|
|
||||||
|
memset(&hints, 0, sizeof hints);
|
||||||
|
hints.ai_family = AF_UNSPEC;
|
||||||
|
hints.ai_socktype = SOCK_STREAM;
|
||||||
|
hints.ai_flags = AI_PASSIVE;
|
||||||
|
|
||||||
|
if ((status = getaddrinfo(addr, port_str, &hints, &res)) != 0)
|
||||||
|
{
|
||||||
|
LOG_ERROR("getaddrinfo: %s\n", gai_strerror(status));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
||||||
|
|
||||||
|
if ((status = bind(fd, res->ai_addr, res->ai_addrlen)) == -1)
|
||||||
|
{
|
||||||
|
LOG_ERROR("error binding socket: %s\n", strerror(errno));
|
||||||
|
freeaddrinfo(res);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((status = listen(fd, max_client_backlog)) == -1)
|
||||||
|
{
|
||||||
|
LOG_ERROR("error setting up listener: %s\n", strerror(errno));
|
||||||
|
freeaddrinfo(res);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
freeaddrinfo(res);
|
||||||
|
|
||||||
|
return fd;
|
||||||
|
}
|
||||||
39
src/helpers/connect_server.c
Normal file
39
src/helpers/connect_server.c
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
|
#include <logger.h>
|
||||||
|
#include <helpers.h>
|
||||||
|
|
||||||
|
int
|
||||||
|
helper_connect_tcp_server(char* host, uint16_t port)
|
||||||
|
{
|
||||||
|
char port_str[6];
|
||||||
|
sprintf(port_str, "%d", port);
|
||||||
|
|
||||||
|
int s, status;
|
||||||
|
struct addrinfo hints, *res;
|
||||||
|
memset(&hints, 0, sizeof hints);
|
||||||
|
hints.ai_family = AF_INET; //set IP Protocol flag (IPv4 or IPv6 - we don't care)
|
||||||
|
hints.ai_socktype = SOCK_STREAM; //set socket flag
|
||||||
|
|
||||||
|
if ((status = getaddrinfo(host, port_str, &hints, &res)) != 0) { //getaddrinfo() will evaluate the given address, using the hints-flags and port, and return an IP address and other server infos
|
||||||
|
LOG_ERROR("getaddrinfo: %s\n", gai_strerror(status));
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//res got filled out by getaddrinfo() for us
|
||||||
|
s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); //creating Socket
|
||||||
|
|
||||||
|
if ((status = connect(s, res->ai_addr, res->ai_addrlen)) != 0) {
|
||||||
|
LOG_ERROR("connect() failed\n");
|
||||||
|
freeaddrinfo(res);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
freeaddrinfo(res);
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}
|
||||||
24
src/helpers/get_port.c
Normal file
24
src/helpers/get_port.c
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <helpers.h>
|
||||||
|
#include <logger.h>
|
||||||
|
|
||||||
|
uint16_t
|
||||||
|
helper_get_port(int sock)
|
||||||
|
{
|
||||||
|
struct sockaddr_in sin;
|
||||||
|
socklen_t len = sizeof(sin);
|
||||||
|
if (getsockname(sock, (struct sockaddr *)&sin, &len) == -1)
|
||||||
|
{
|
||||||
|
LOG_ERROR("could not get socket name for port: %s\n", strerror(errno));
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return ntohs(sin.sin_port);
|
||||||
|
}
|
||||||
|
}
|
||||||
11
src/helpers/get_weekday.c
Normal file
11
src/helpers/get_weekday.c
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
|
#include <helpers.h>
|
||||||
|
|
||||||
|
int
|
||||||
|
helper_get_weekday(const struct tm *time_struct)
|
||||||
|
{
|
||||||
|
int wday_sun_sat = time_struct->tm_wday;
|
||||||
|
int wday_mon_sun = (wday_sun_sat + 6) % 7;
|
||||||
|
return wday_mon_sun;
|
||||||
|
}
|
||||||
115
src/helpers/load_config.c
Normal file
115
src/helpers/load_config.c
Normal file
|
|
@ -0,0 +1,115 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <helpers.h>
|
||||||
|
#include <config.h>
|
||||||
|
#include <logger.h>
|
||||||
|
#include <confini.h>
|
||||||
|
|
||||||
|
#define CONFINI_IS_KEY(SECTION, KEY) \
|
||||||
|
(ini_array_match(SECTION, disp->append_to, '.', disp->format) && \
|
||||||
|
ini_string_match_ii(KEY, disp->data, disp->format))
|
||||||
|
|
||||||
|
int
|
||||||
|
helper_load_config(IniDispatch *disp, void *config_void)
|
||||||
|
{
|
||||||
|
config_t *config = (config_t*)config_void;
|
||||||
|
char relay_section_name[10]; // "relay-255\0" is longest name
|
||||||
|
|
||||||
|
if(disp->type == INI_KEY)
|
||||||
|
{
|
||||||
|
if(CONFINI_IS_KEY("controller", "name"))
|
||||||
|
{
|
||||||
|
strncpy(config->name, disp->value, MAX_NAME_LENGTH);
|
||||||
|
config->name[MAX_NAME_LENGTH] = '\0';
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if(CONFINI_IS_KEY("controller", "database"))
|
||||||
|
{
|
||||||
|
config->database = malloc(sizeof(char) * (strlen(disp->value) + 1));
|
||||||
|
strcpy(config->database, disp->value);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if(CONFINI_IS_KEY("controller", "log-level"))
|
||||||
|
{
|
||||||
|
if(strcasecmp(disp->value, "trace") == 0)
|
||||||
|
{
|
||||||
|
config->log_level = LOG_LEVEL_TRACE;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if(strcasecmp(disp->value, "debug") == 0)
|
||||||
|
{
|
||||||
|
config->log_level = LOG_LEVEL_DEBUG;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if(strcasecmp(disp->value, "info") == 0)
|
||||||
|
{
|
||||||
|
config->log_level = LOG_LEVEL_INFO;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if(strcasecmp(disp->value, "warn") == 0)
|
||||||
|
{
|
||||||
|
config->log_level = LOG_LEVEL_WARN;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if(strcasecmp(disp->value, "error") == 0)
|
||||||
|
{
|
||||||
|
config->log_level = LOG_LEVEL_ERROR;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if(strcasecmp(disp->value, "fatal") == 0)
|
||||||
|
{
|
||||||
|
config->log_level = LOG_LEVEL_FATAL;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
LOG_WARN("invalid log-level '%s'\n", disp->value);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if(CONFINI_IS_KEY("controller", "discovery-port"))
|
||||||
|
{
|
||||||
|
config->discovery_port = atoi(disp->value);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if(CONFINI_IS_KEY("controller", "relay-count"))
|
||||||
|
{
|
||||||
|
config->relay_count = atoi(disp->value);
|
||||||
|
config->relay_configs = malloc(sizeof(config_relay_t) * config->relay_count);
|
||||||
|
for(uint8_t i = 0; i < config->relay_count; ++i)
|
||||||
|
{
|
||||||
|
config->relay_configs[i].driver= RELAY_DRIVER_NONE;
|
||||||
|
}
|
||||||
|
LOG_TRACE("config relay-count set to %u\n", config->relay_count);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
for(uint8_t i = 0; i < config->relay_count; ++i)
|
||||||
|
{
|
||||||
|
sprintf(relay_section_name, "relay-%u", i);
|
||||||
|
if(CONFINI_IS_KEY(relay_section_name, "pin"))
|
||||||
|
{
|
||||||
|
config->relay_configs[i].pin = atoi(disp->value);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if(CONFINI_IS_KEY(relay_section_name, "inverted"))
|
||||||
|
{
|
||||||
|
config->relay_configs[i].inverted = atoi(disp->value);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if(CONFINI_IS_KEY(relay_section_name, "driver"))
|
||||||
|
{
|
||||||
|
if(strcasecmp(disp->value, "gpio") == 0)
|
||||||
|
{
|
||||||
|
config->relay_configs[i].driver = RELAY_DRIVER_GPIO;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if(strcasecmp(disp->value, "piface") == 0)
|
||||||
|
{
|
||||||
|
config->relay_configs[i].driver = RELAY_DRIVER_PIFACE;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
LOG_WARN("invalid driver '%s' in section '%s'\n", disp->value, relay_section_name);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
57
src/helpers/open_discovery_socket.c
Normal file
57
src/helpers/open_discovery_socket.c
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
|
#include <logger.h>
|
||||||
|
#include <helpers.h>
|
||||||
|
|
||||||
|
int
|
||||||
|
helper_open_discovery_socket(uint16_t discovery_port)
|
||||||
|
{
|
||||||
|
struct addrinfo hints, *res;
|
||||||
|
int fd, status;
|
||||||
|
|
||||||
|
memset(&hints, 0, sizeof hints);
|
||||||
|
hints.ai_family = AF_INET; // use ipv4
|
||||||
|
hints.ai_socktype = SOCK_DGRAM; //set socket flag
|
||||||
|
hints.ai_flags = AI_PASSIVE; // get my IP
|
||||||
|
|
||||||
|
char discovery_port_str[6];
|
||||||
|
sprintf(discovery_port_str, "%u", discovery_port);
|
||||||
|
|
||||||
|
//get connection info for our computer
|
||||||
|
if ((status = getaddrinfo(NULL, discovery_port_str, &hints, &res)) != 0)
|
||||||
|
{
|
||||||
|
LOG_FATAL("getaddrinfo: %s\n", gai_strerror(status));
|
||||||
|
freeaddrinfo(res);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
//creating socket
|
||||||
|
fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
|
||||||
|
int yes = 1;
|
||||||
|
|
||||||
|
// lose the pesky "Address already in use" error message
|
||||||
|
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) == -1)
|
||||||
|
{
|
||||||
|
LOG_FATAL("setsockopt: %s\n", strerror(errno));
|
||||||
|
freeaddrinfo(res);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (bind(fd, res->ai_addr, res->ai_addrlen) == -1)
|
||||||
|
{
|
||||||
|
LOG_FATAL("bind: %s\n", strerror(errno));
|
||||||
|
freeaddrinfo(res);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
freeaddrinfo(res);
|
||||||
|
|
||||||
|
LOG_INFO("opened discovery socket on port %u\n", discovery_port);
|
||||||
|
|
||||||
|
return fd;
|
||||||
|
}
|
||||||
70
src/helpers/parse_cli.c
Normal file
70
src/helpers/parse_cli.c
Normal file
|
|
@ -0,0 +1,70 @@
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include <argparse.h>
|
||||||
|
#include <config.h>
|
||||||
|
#include <logger.h>
|
||||||
|
#include <helpers.h>
|
||||||
|
#include <version.h>
|
||||||
|
|
||||||
|
static const char *const usage[] = {
|
||||||
|
"controller [options] [[--] args]",
|
||||||
|
"controller [options]",
|
||||||
|
NULL,
|
||||||
|
};
|
||||||
|
|
||||||
|
#define PERM_READ (1<<0)
|
||||||
|
#define PERM_WRITE (1<<1)
|
||||||
|
#define PERM_EXEC (1<<2)
|
||||||
|
|
||||||
|
void
|
||||||
|
helper_parse_cli(int argc, const char **argv, config_t *config)
|
||||||
|
{
|
||||||
|
int version = 0;
|
||||||
|
struct argparse_option options[] =
|
||||||
|
{
|
||||||
|
OPT_HELP(),
|
||||||
|
OPT_GROUP("Basic options"),
|
||||||
|
OPT_STRING('c', "config", &config->file, "path to config file", NULL, 0, OPT_NONEG),
|
||||||
|
OPT_BOOLEAN('v', "version", &version, "print version", NULL, 0, OPT_NONEG),
|
||||||
|
OPT_END(),
|
||||||
|
};
|
||||||
|
|
||||||
|
struct argparse argparse;
|
||||||
|
argparse_init(&argparse, options, usage, 0);
|
||||||
|
argparse_describe(
|
||||||
|
&argparse,
|
||||||
|
"\nA brief description of what the program does and how it works.",
|
||||||
|
"\nAdditional description of the program after the description of the arguments."
|
||||||
|
);
|
||||||
|
argc = argparse_parse(&argparse, argc, argv);
|
||||||
|
|
||||||
|
if(version)
|
||||||
|
{
|
||||||
|
printf("%s\n", EMGAUWA_CONTROLLER_VERSION);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(argc == 1)
|
||||||
|
{
|
||||||
|
if(strcmp(argv[0], "start") == 0)
|
||||||
|
{
|
||||||
|
config->run_type = RUN_TYPE_START;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if(strcmp(argv[0], "test") == 0)
|
||||||
|
{
|
||||||
|
config->run_type = RUN_TYPE_TEST;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
LOG_FATAL("bad action '%s' given ('start', 'test')\n", argv[0]);
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LOG_FATAL("no action given ('start', 'test')\n");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue