diff --git a/include/cfg.h b/include/cfg.h index ec4e7055b..12a442b9a 100644 --- a/include/cfg.h +++ b/include/cfg.h @@ -13,27 +13,25 @@ struct node; struct path; -/// Global configuration +/** Global configuration */ struct config { - /// Name of this node + /** Name of this node */ const char *name; - /// Configuration filename + /** Configuration filename */ const char *filename; - /// Verbosity level - int debug; - /// Task priority (lower is better) + /** Task priority (lower is better) */ int priority; - /// Core affinity of this task + /** Core affinity of this task */ int affinity; - /// Protocol version of UDP packages + /** Protocol version of UDP packages */ int protocol; /// Number of parsed paths int path_count; /// Number of parsed nodes int node_count; - /// libconfig object - config_t obj; + /** A libconfig object pointing to the root of the config file */ + config_setting_t *cfg; /// Array of nodes struct node *nodes; @@ -45,6 +43,9 @@ struct config { * * @param c A libconfig object * @param g The global configuration structure (also contains the config filename) + * @return + * - 0 on success + * - otherwise an error occured */ int config_parse(config_t *c, struct config *g); diff --git a/include/config.h b/include/config.h index 42552f26f..241aec0b0 100644 --- a/include/config.h +++ b/include/config.h @@ -10,19 +10,19 @@ #ifndef _CONFIG_H_ #define _CONFIG_H_ -/// The version number of the s2ss server +/** The version number of the s2ss server */ #define VERSION __GIT_TAG__ "-" __GIT_REV__ -/// Maximum number of double values in a struct msg +/** Maximum number of double values in a struct msg */ #define MAX_VALUES 6 -/// Maximum number of registrable hook functions per path +/** Maximum number of registrable hook functions per path */ #define MAX_HOOKS 5 -/// Size of the stack which gets prefaulted during initialization +/** Size of the stack which gets prefaulted during initialization */ #define MAX_SAFE_STACK (16*1024) /* 16 KiB */ -/// Socket priority +/** Socket priority */ #define SOCKET_PRIO 7 #endif /* _CONFIG_H_ */ diff --git a/include/msg.h b/include/msg.h index 8b94d2ff9..e2344fe17 100644 --- a/include/msg.h +++ b/include/msg.h @@ -22,13 +22,13 @@ */ struct msg { - /// Sender device ID + /** Sender device ID */ uint16_t device; - /// Message ID + /** Message ID */ uint32_t sequence; - /// Message length (data only) + /** Message length (data only) */ uint16_t length; - /// Message data + /** Message data */ double data[MAX_VALUES]; } __attribute__((packed)); #elif PROTOCOL == 1 @@ -41,21 +41,21 @@ struct msg { struct { - /// Protocol version + /** Protocol version */ unsigned version : 4; - /// Header length + /** Header length */ unsigned hdr_len : 4; - /// Message flags + /** Message flags */ uint8_t flags; - /// Sender device ID + /** Sender device ID */ uint16_t dev_id; - /// Message ID + /** Message ID */ uint32_t sequence; - /// Message length (data only) + /** Message length (data only) */ uint16_t data_len; - /// Digital signature for authentication + /** Digital signature for authentication */ uint32_t signature; - /// Timestamp in uS since unix epoch + /** Timestamp in uS since unix epoch */ uint64_t timestamp } header; union diff --git a/include/node.h b/include/node.h index d03fd8bda..5886d41a0 100644 --- a/include/node.h +++ b/include/node.h @@ -16,6 +16,7 @@ #include #include +#include /** The type of a node. * @@ -37,26 +38,29 @@ enum node_type */ struct node { - /// The socket descriptor + /** The socket descriptor */ int sd; - /// The type of this node + /** The type of this node */ enum node_type type; - /// Local address of the socket + /** Local address of the socket */ struct sockaddr_in local; - /// Remote address of the socket + /** Remote address of the socket */ struct sockaddr_in remote; - /// Name of the local interface + /** Name of the local interface */ const char *ifname; - /// Index of the local interface + /** Index of the local interface */ int ifindex; /// Socket Mark int mark; - /// A short identifier of the node + /** A short identifier of the node */ const char *name; + + /** A pointer to the libconfig object which instantiated this node */ + config_setting_t *cfg; }; /** Create a new node. diff --git a/include/path.h b/include/path.h index 0e575db3c..4bc06fe28 100644 --- a/include/path.h +++ b/include/path.h @@ -9,6 +9,7 @@ #define _PATH_H_ #include +#include #include "config.h" #include "node.h" @@ -20,9 +21,9 @@ */ struct path { - /// Pointer to the incoming node + /** Pointer to the incoming node */ struct node *in; - /// Pointer to the outgoing node + /** Pointer to the outgoing node */ struct node *out; /** If non NULL this function is called for every received message. @@ -32,20 +33,19 @@ struct path */ int (*hook)(struct msg *m); - /// Counter for received messages + /** Counter for received messages */ unsigned int received; - - /// Counter for messages which arrived reordered + /** Counter for messages which arrived reordered */ unsigned int delayed; - - /// Counter for messages which arrived multiple times + /** Counter for messages which arrived multiple times */ unsigned int duplicated; - - /// Last known message number + /** Last known message number */ unsigned int sequence; - /// The thread for this path + /** The thread for this path */ pthread_t tid; + /** A pointer to the libconfig object which instantiated this path */ + config_setting_t *cfg; }; /** Setup a new path. diff --git a/include/utils.h b/include/utils.h index 12d9bce2e..f3d752045 100644 --- a/include/utils.h +++ b/include/utils.h @@ -17,7 +17,7 @@ struct config; struct sockaddr_in; struct sockaddr; -/// The log level which is passed as first argument to print() +/** The log level which is passed as first argument to print() */ enum log_level { DEBUG, @@ -69,7 +69,7 @@ void init_realtime(struct config *g); */ int sockaddr_cmp(struct sockaddr *a, struct sockaddr *b); -/// Check assertion and exit if failed. +/** Check assertion and exit if failed. */ #define assert(exp) do { \ if (!(exp)) { \ print(ERROR, "Assertion failed: '%s' in %s, %s:%d", \ @@ -77,36 +77,36 @@ int sockaddr_cmp(struct sockaddr *a, struct sockaddr *b); exit(EXIT_FAILURE); \ } } while (0) -/// Printf alike debug message with level. +/** Printf alike debug message with level. */ #define debug(lvl, msg, ...) do { \ if (lvl <= V) \ print(DEBUG, msg, ##__VA_ARGS__); \ } while (0) -/// Printf alike info message. +/** Printf alike info message. */ #define info(msg, ...) do { \ print(INFO, msg, ##__VA_ARGS__); \ } while (0) -/// Printf alike warning message. +/** Printf alike warning message. */ #define warn(msg, ...) do { \ print(WARN, msg, ##__VA_ARGS__); \ } while (0) -/// Print error and exit. +/** Print error and exit. */ #define error(msg, ...) do { \ print(ERROR, msg, ##__VA_ARGS__); \ exit(EXIT_FAILURE); \ } while (0) -/// Print error and strerror(errno). +/** Print error and strerror(errno). */ #define perror(msg, ...) do { \ print(ERROR, msg ": %s", ##__VA_ARGS__, \ strerror(errno)); \ exit(EXIT_FAILURE); \ } while (0) -/// Print configuration error and exit. +/** Print configuration error and exit. */ #define cerror(c, msg, ...) do { \ print(ERROR, msg " in %s:%u", ##__VA_ARGS__, \ config_setting_source_file(c), \ diff --git a/src/cfg.c b/src/cfg.c index 2839bf394..22b74df09 100644 --- a/src/cfg.c +++ b/src/cfg.c @@ -1,7 +1,7 @@ /** * Configuration parser * - * @author Steffen Vogel + * @author Steffen Vogel * @copyright 2014, Institute for Automation of Complex Power Systems, EONERC */ @@ -76,6 +76,8 @@ int config_parse_global(config_setting_t *c, struct config *g) config_setting_lookup_int(c, "priority", &g->priority); config_setting_lookup_int(c, "protocol", &g->protocol); + g->cfg = c; + return CONFIG_TRUE; } @@ -112,6 +114,7 @@ int config_parse_path(config_setting_t *c, struct config *g) if (path_create(&g->paths[g->path_count], in, out)) cerror(c, "Failed to parse path"); + g->cfg = c; g->path_count++; if (reverse) { @@ -132,8 +135,7 @@ int config_parse_node(config_setting_t *c, struct config *g) const char *remote_str = NULL; const char *local_str = NULL; - struct sockaddr_in local; - struct sockaddr_in remote; + struct sockaddr_in local, remote; enum node_type type; /* Optional settings */ @@ -168,5 +170,6 @@ int config_parse_node(config_setting_t *c, struct config *g) if (node_create(&g->nodes[g->node_count], name, type, local, remote)) cerror(c, "Failed to parse node"); + g->cfg = c; g->node_count++; } diff --git a/src/server.c b/src/server.c index 4d91bcc7c..697992f38 100644 --- a/src/server.c +++ b/src/server.c @@ -20,9 +20,8 @@ #include "path.h" #include "node.h" -/// Global default settings -struct config config = { - .debug = 0, +/** Default settings */ +static struct config config = { .priority = 0, .affinity = 0xC0, .protocol = 0