From c215d96ee6543fbc9429a1962c7d7b58593977ea Mon Sep 17 00:00:00 2001 From: Bui Nguyen Quoc Thanh Date: Thu, 20 Feb 2020 18:11:43 +0700 Subject: gateway: Configurable interval time - Introduce "General" section for dlt_gateway.conf - In new section, the interval is used for gateway timer. This entry is optional. By default, it is set to 1 second as the existing hardcoded value. Signed-off-by: Bui Nguyen Quoc Thanh --- src/daemon/dlt-daemon.c | 4 +- src/gateway/dlt_gateway.c | 191 ++++++++++++++++++++++++++++-------- src/gateway/dlt_gateway.conf | 6 +- src/gateway/dlt_gateway_types.h | 15 ++- src/shared/dlt_config_file_parser.c | 3 + 5 files changed, 172 insertions(+), 47 deletions(-) diff --git a/src/daemon/dlt-daemon.c b/src/daemon/dlt-daemon.c index e4039a2..aff33e6 100644 --- a/src/daemon/dlt-daemon.c +++ b/src/daemon/dlt-daemon.c @@ -882,8 +882,8 @@ int main(int argc, char *argv[]) /* create gateway timer */ create_timer_fd(&daemon_local, - DLT_GATEWAY_TIMER_INTERVAL, - DLT_GATEWAY_TIMER_INTERVAL, + daemon_local.pGateway.interval, + daemon_local.pGateway.interval, DLT_TIMER_GATEWAY); } diff --git a/src/gateway/dlt_gateway.c b/src/gateway/dlt_gateway.c index 6680ac8..a47563a 100644 --- a/src/gateway/dlt_gateway.c +++ b/src/gateway/dlt_gateway.c @@ -199,6 +199,29 @@ DLT_STATIC DltReturnValue dlt_gateway_check_timeout(DltGatewayConnection *con, return DLT_RETURN_ERROR; } +/** + * Check connection interval value in General section + * + * @param con DltGateway to be updated + * @param value string to be tested + * @return Value from DltReturnValue enum + */ +DLT_STATIC DltReturnValue dlt_gateway_check_interval(DltGateway *gateway, + char *value) +{ + if ((gateway == NULL) || (value == NULL)) { + dlt_vlog(LOG_ERR, "%s: wrong parameter\n", __func__); + return DLT_RETURN_WRONG_PARAMETER; + } + + gateway->interval = (int)strtol(value, NULL, 10); + + if (gateway->interval > 0) + return DLT_RETURN_OK; + + return DLT_RETURN_ERROR; +} + /** * Check the value for SendSerialHeader * @@ -492,8 +515,39 @@ DLT_STATIC DltGatewayConf configuration_entries[GW_CONF_COUNT] = { } }; +DLT_STATIC DltGatewayGeneralConf general_entries[GW_CONF_COUNT] = { + [GW_CONF_GENERAL_INTERVAL] = { + .key = "Interval", + .func = dlt_gateway_check_interval, + .is_opt = 1 + } +}; + #define DLT_GATEWAY_NUM_PROPERTIES_MAX GW_CONF_COUNT +/** + * Check if gateway connection general configuration parameter is valid. + * + * @param gateway DltGateway + * @param ctype DltGatwayGeneralConnection property + * @param value specified property value from configuration file + * @return Value from DltReturnValue enum + */ +DLT_STATIC DltReturnValue dlt_gateway_check_general_param(DltGateway *gateway, + DltGatewayGeneralConfType ctype, + char *value) +{ + if ((gateway == NULL) || (value == NULL)) { + dlt_vlog(LOG_ERR, "%s: wrong parameter\n", __func__); + return DLT_RETURN_WRONG_PARAMETER; + } + + if (ctype < GW_CONF_GENEREL_COUNT) + return general_entries[ctype].func(gateway, value); + + return DLT_RETURN_ERROR; +} + /** * Check if gateway connection configuration parameter is valid. * @@ -603,6 +657,7 @@ int dlt_gateway_configure(DltGateway *gateway, char *config_file, int verbose) int ret = 0; int i = 0; DltConfigFile *file = NULL; + int num_sections = 0; PRINT_FUNCTION_VERBOSE(verbose); @@ -615,7 +670,7 @@ int dlt_gateway_configure(DltGateway *gateway, char *config_file, int verbose) file = dlt_config_file_init(config_file); /* get number of entries and allocate memory to store information */ - ret = dlt_config_file_get_num_sections(file, &gateway->num_connections); + ret = dlt_config_file_get_num_sections(file, &num_sections); if (ret != 0) { dlt_config_file_release(file); @@ -623,6 +678,11 @@ int dlt_gateway_configure(DltGateway *gateway, char *config_file, int verbose) return DLT_RETURN_ERROR; } + /* + * Since the General section is also counted in num_sections, + * so number of connections must be number of sections subtracts 1. + */ + gateway->num_connections = num_sections - 1; gateway->connections = calloc(sizeof(DltGatewayConnection), gateway->num_connections); @@ -632,10 +692,11 @@ int dlt_gateway_configure(DltGateway *gateway, char *config_file, int verbose) return DLT_RETURN_ERROR; } - for (i = 0; i < gateway->num_connections; i++) { + for (i = 0; i < num_sections; i++) { DltGatewayConnection tmp; int invalid = 0; DltGatewayConfType j = 0; + DltGatewayGeneralConfType g = 0; char section[DLT_CONFIG_FILE_ENTRY_MAX_LEN] = { '\0' }; char value[DLT_CONFIG_FILE_ENTRY_MAX_LEN] = { '\0' }; @@ -646,57 +707,100 @@ int dlt_gateway_configure(DltGateway *gateway, char *config_file, int verbose) tmp.port = DLT_DAEMON_TCP_PORT; ret = dlt_config_file_get_section_name(file, i, section); + if (ret != 0) { + dlt_log(LOG_WARNING, "Get section name failed\n"); + continue; + } - for (j = 0; j < GW_CONF_COUNT; j++) { - ret = dlt_config_file_get_value(file, - section, - configuration_entries[j].key, - value); + if (strncmp(section, DLT_GATEWAY_GENERAL_SECTION_NAME, + sizeof(DLT_GATEWAY_GENERAL_SECTION_NAME)) == 0) { + for (g = 0; g < GW_CONF_GENEREL_COUNT; g++) { + ret = dlt_config_file_get_value(file, + section, + general_entries[g].key, + value); - if ((ret != 0) && configuration_entries[j].is_opt) { - /* Use default values for this key */ - dlt_vlog(LOG_WARNING, - "Using default for %s.\n", - configuration_entries[j].key); - continue; - } - else if (ret != 0) - { - dlt_vlog(LOG_WARNING, - "Missing configuration for %s.\n", - configuration_entries[j].key); - invalid = 1; - break; - } - - /* check value and store temporary */ - ret = dlt_gateway_check_param(gateway, &tmp, j, value); + if ((ret != 0) && general_entries[g].is_opt) { + /* Use default values for this key */ + dlt_vlog(LOG_WARNING, + "Using default for %s.\n", + general_entries[g].key); + continue; + } + else if (ret != 0) + { + dlt_vlog(LOG_WARNING, + "Missing configuration for %s.\n", + general_entries[g].key); + break; + } - if (ret != 0) - dlt_vlog(LOG_ERR, - "Configuration %s = %s is invalid.\n" - "Using default.\n", - configuration_entries[j].key, value); - } + /* check value and store general configuration */ + ret = dlt_gateway_check_general_param(gateway, g, value); - if (invalid) { - dlt_vlog(LOG_ERR, - "%s configuration is invalid.\n" - "Ignoring.\n", - section); + if (ret != 0) + dlt_vlog(LOG_ERR, + "Configuration %s = %s is invalid.\n" + "Using default.\n", + general_entries[g].key, value); + } } else { - ret = dlt_gateway_store_connection(gateway, &tmp, verbose); + for (j = 0; j < GW_CONF_COUNT; j++) { + ret = dlt_config_file_get_value(file, + section, + configuration_entries[j].key, + value); + + if ((ret != 0) && configuration_entries[j].is_opt) { + /* Use default values for this key */ + dlt_vlog(LOG_WARNING, + "Using default for %s.\n", + configuration_entries[j].key); + continue; + } + else if (ret != 0) + { + dlt_vlog(LOG_WARNING, + "Missing configuration for %s.\n", + configuration_entries[j].key); + invalid = 1; + break; + } - if (ret != 0) - dlt_log(LOG_ERR, "Storing gateway connection data failed\n"); + /* check value and store temporary */ + ret = dlt_gateway_check_param(gateway, &tmp, j, value); + + if (ret != 0) + dlt_vlog(LOG_ERR, + "Configuration %s = %s is invalid.\n" + "Using default.\n", + configuration_entries[j].key, value); + } + + if (invalid) { + dlt_vlog(LOG_ERR, + "%s configuration is invalid.\n" + "Ignoring.\n", + section); + } + else { + ret = dlt_gateway_store_connection(gateway, &tmp, verbose); + + if (ret != 0) + dlt_log(LOG_ERR, "Storing gateway connection data failed\n"); + } } /* strdup used inside some get_value function */ - free(tmp.ecuid); - tmp.ecuid = NULL; - free(tmp.ip_address); - tmp.ip_address = NULL; + if (tmp.ecuid != NULL) { + free(tmp.ecuid); + tmp.ecuid = NULL; + } + if (tmp.ip_address != NULL) { + free(tmp.ip_address); + tmp.ip_address = NULL; + } } dlt_config_file_release(file); @@ -717,6 +821,7 @@ int dlt_gateway_init(DltDaemonLocal *daemon_local, int verbose) if (gateway != NULL) { /* Get default value from daemon_local */ gateway->send_serial = daemon_local->flags.lflag; + gateway->interval = DLT_GATEWAY_TIMER_DEFAULT_INTERVAL; if (dlt_gateway_configure(gateway, daemon_local->flags.gatewayConfigFile, diff --git a/src/gateway/dlt_gateway.conf b/src/gateway/dlt_gateway.conf index c68aa4f..c7ef3a8 100644 --- a/src/gateway/dlt_gateway.conf +++ b/src/gateway/dlt_gateway.conf @@ -1,3 +1,7 @@ +[General] +; Time interval for reconnection to passive Node in second. +; Interval=1 + [PassiveNode1] IPaddress=192.168.2.11 ; TCP port. Default 3490 is used if no port is specified. @@ -6,7 +10,7 @@ IPaddress=192.168.2.11 EcuID=ECU2 ; Try connect to passive Node on DLT Daemon startup. Default OnStartup if not specified. ; Connect=OnStartup -; Stop connecting to passive node, if not successful after 10 seconds +; Stop connecting to passive node, if not successful after 10 retries. Timeout=10 ; Send following control messages after connection is established ; SendControl=0x03,0x04,0x13 diff --git a/src/gateway/dlt_gateway_types.h b/src/gateway/dlt_gateway_types.h index 0fca7cf..1a7f645 100644 --- a/src/gateway/dlt_gateway_types.h +++ b/src/gateway/dlt_gateway_types.h @@ -60,7 +60,8 @@ #include "dlt_client.h" #define DLT_GATEWAY_CONFIG_PATH CONFIGURATION_FILES_DIR "/dlt_gateway.conf" -#define DLT_GATEWAY_TIMER_INTERVAL 1 +#define DLT_GATEWAY_TIMER_DEFAULT_INTERVAL 1 +#define DLT_GATEWAY_GENERAL_SECTION_NAME "General" #define DLT_GATEWAY_RECONNECT_MAX 1 /* reconnect once after connection loss */ @@ -139,6 +140,7 @@ typedef struct int send_serial; /* Default: Send serial header with control messages */ DltGatewayConnection *connections; /* pointer to connections */ int num_connections; /* number of connections */ + int interval; /* interval of retry connection */ } DltGateway; typedef struct { @@ -147,6 +149,12 @@ typedef struct { int is_opt; /* If the configuration is optional or not */ } DltGatewayConf; +typedef struct { + char *key; /* The configuration key*/ + int (*func)(DltGateway *gateway, char *value); /* Conf handler */ + int is_opt; /* If the configuration is optional or not */ +} DltGatewayGeneralConf; + typedef enum { GW_CONF_IP_ADDRESS = 0, GW_CONF_PORT, @@ -159,4 +167,9 @@ typedef enum { GW_CONF_COUNT } DltGatewayConfType; +typedef enum { + GW_CONF_GENERAL_INTERVAL = 0, + GW_CONF_GENEREL_COUNT +} DltGatewayGeneralConfType; + #endif /* DLT_GATEWAY_TYPES_H_ */ diff --git a/src/shared/dlt_config_file_parser.c b/src/shared/dlt_config_file_parser.c index 7392836..b123ebf 100644 --- a/src/shared/dlt_config_file_parser.c +++ b/src/shared/dlt_config_file_parser.c @@ -504,6 +504,9 @@ int dlt_config_file_get_num_sections(const DltConfigFile *file, int *num) if ((file == NULL) || (file->num_sections < 0)) return -1; + /* + * Note: this number is also containing General section + */ *num = file->num_sections; return 0; -- cgit v1.2.1