Skip to content

Commit

Permalink
Configuration reading improvements
Browse files Browse the repository at this point in the history
Signed-off-by: Mustafa Özcelikörs <[email protected]>
  • Loading branch information
mozcelikors committed Jan 31, 2018
1 parent 19fe41e commit a9d938b
Show file tree
Hide file tree
Showing 4 changed files with 290 additions and 77 deletions.
22 changes: 22 additions & 0 deletions include/libraries/config_reader/config_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,21 @@
#define DELIM "="
#define ROVER_CONFIG_FILE "/etc/rover.conf"

#define DEFAULT_ROVER_IDENTITY_C (int) 1
#define DEFAULT_MQTT_BROKER_C (char*) "127.0.0.1"
#define DEFAULT_MQTT_BROKER_PORT_C (int) 1883
#define DEFAULT_ROVER_MQTT_QOS_C (int) 0
#define DEFAULT_MQTT_USERNAME_C (char*) "sensor1@DEFAULT_TENANT"
#define DEFAULT_MQTT_PASSWORD_C (char*) "hono-secret"
#define DEFAULT_USE_GROOVE_SENSOR_C (int) 0
#define DEFAULT_HONO_HTTP_HOST_C (char*) "idial.institute"
#define DEFAULT_HONO_HTTP_PORT_C (int) 8080
#define DEFAULT_HONO_HTTP_TENANT_NAME_C (char*) "DEFAULT_TENANT"
#define DEFAULT_HONO_HTTP_DEVICE_ID_C (char*) "4711"
#define DEFAULT_HONO_HTTP_REGISTER_PORT_C (int) 28080
#define DEFAULT_HONO_HTTP_USERNAME_C (char*) "sensor1"
#define DEFAULT_HONO_HTTP_PASSWORD_C (char*) "hono-secret"

typedef struct
{
int ROVER_IDENTITY_C;
Expand All @@ -31,6 +46,13 @@ typedef struct
char MQTT_USERNAME_C [ROVER_CONFIG_MAXBUF];
char MQTT_PASSWORD_C [ROVER_CONFIG_MAXBUF];
int USE_GROOVE_SENSOR_C;
char HONO_HTTP_HOST_C [ROVER_CONFIG_MAXBUF];
int HONO_HTTP_PORT_C;
char HONO_HTTP_TENANT_NAME_C [ROVER_CONFIG_MAXBUF];
char HONO_HTTP_DEVICE_ID_C [ROVER_CONFIG_MAXBUF];
int HONO_HTTP_REGISTER_PORT_C;
char HONO_HTTP_USERNAME_C [ROVER_CONFIG_MAXBUF];
char HONO_HTTP_PASSWORD_C [ROVER_CONFIG_MAXBUF];
} rover_config;

rover_config getRoverConfig (char *filename);
Expand Down
9 changes: 8 additions & 1 deletion samples/rover.conf.sample
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,11 @@ MQTT_BROKER_PORT_C=1883
ROVER_MQTT_QOS_C=0
MQTT_USERNAME_C=sensor1@DEFAULT_TENANT
MQTT_PASSWORD_C=hono-secret
USE_GROOVE_SENSOR_C=0
USE_GROOVE_SENSOR_C=0
HONO_HTTP_HOST_C=idial.institute
HONO_HTTP_PORT_C=8080
HONO_HTTP_TENANT_NAME_C=DEFAULT_TENANT
HONO_HTTP_DEVICE_ID_C=4711
HONO_HTTP_REGISTER_PORT_C=28080
HONO_HTTP_USERNAME_C=sensor1
HONO_HTTP_PASSWORD_C=hono-secret
308 changes: 246 additions & 62 deletions src/libraries/config_reader/config_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@

rover_config getRoverConfig(char *filename)
{
rover_config configstruct = {1, "127.0.0.1", 1883, 0, "", "", 0};
rover_config configstruct;
int use_defaults = 0;
int i = 0;

FILE *file = fopen (filename, "r");
printf ("Checking /etc/rover.conf\n");

if (file != NULL)
{
char line[ROVER_CONFIG_MAXBUF];
int i = 0;

while(fgets(line, sizeof(line), file) != NULL)
{
Expand All @@ -40,59 +42,210 @@ rover_config getRoverConfig(char *filename)
{
cfline = cfline + strlen(DELIM);

if (i == 2)
{
configstruct.ROVER_IDENTITY_C = atoi(cfline);
printf("Read config [ROVER_IDENTITY_C = %d]\n",configstruct.ROVER_IDENTITY_C);
}
else if (i == 3)
{
memcpy(configstruct.MQTT_BROKER_C,cfline,strlen(cfline));
configstruct.MQTT_BROKER_C[strlen(cfline)-1] = 0;
printf("Read config [MQTT_BROKER_C = %s]\n",configstruct.MQTT_BROKER_C);
}
else if (i == 4)
switch (i)
{
configstruct.MQTT_BROKER_PORT_C = atoi(cfline);
printf("Read config [MQTT_BROKER_PORT_C = %d]\n",configstruct.MQTT_BROKER_PORT_C);
}
else if (i == 5)
{
configstruct.ROVER_MQTT_QOS_C = atoi(cfline);
printf("Read config [ROVER_MQTT_QOS_C = %d]\n",configstruct.ROVER_MQTT_QOS_C);
}
else if (i == 6)
{
if (strlen(cfline) > 1)
{
memcpy(configstruct.MQTT_USERNAME_C,cfline,strlen(cfline));
configstruct.MQTT_USERNAME_C[strlen(cfline)-1] = 0;
printf("Read config [MQTT_USERNAME_C = %s]\n",configstruct.MQTT_USERNAME_C);
}
else
{
printf("Read config [MQTT_USERNAME_C = <no-username>]\n");

}
}
else if (i == 7)
{
if (strlen(cfline) > 1)
{
memcpy(configstruct.MQTT_PASSWORD_C,cfline,strlen(cfline));
configstruct.MQTT_PASSWORD_C[strlen(cfline)-1] = 0;
printf("Read config [MQTT_PASSWORD_C = %s]\n",configstruct.MQTT_PASSWORD_C);
}
else
{
printf("Read config [MQTT_PASSWORD_C = <no-password>]\n");

}
}
else if (i == 8)
{
configstruct.USE_GROOVE_SENSOR_C = atoi(cfline);
printf("Read config [USE_GROOVE_SENSOR_C = %d]\n",configstruct.USE_GROOVE_SENSOR_C);
case 2:
if (strlen(cfline) > 1)
{
configstruct.ROVER_IDENTITY_C = atoi(cfline);
printf("Read config [ROVER_IDENTITY_C = %d]\n",configstruct.ROVER_IDENTITY_C);
}
else
{
configstruct.ROVER_IDENTITY_C = DEFAULT_ROVER_IDENTITY_C;
printf("No data entered. Using default: [ROVER_IDENTITY_C = %d]\n",configstruct.ROVER_IDENTITY_C);
}
break;

case 3:
if (strlen(cfline) > 1)
{
memcpy(configstruct.MQTT_BROKER_C,cfline,strlen(cfline));
configstruct.MQTT_BROKER_C[strlen(cfline)-1] = 0;
printf("Read config [MQTT_BROKER_C = %s]\n",configstruct.MQTT_BROKER_C);
}
else
{
memcpy(configstruct.MQTT_BROKER_C,DEFAULT_MQTT_BROKER_C,strlen(DEFAULT_MQTT_BROKER_C));
configstruct.MQTT_BROKER_C[strlen(DEFAULT_MQTT_BROKER_C)-1] = 0;
printf("No data entered. Using default: [MQTT_BROKER_C = %s]\n",configstruct.MQTT_BROKER_C);
}
break;

case 4:
if (strlen(cfline) > 1)
{
configstruct.MQTT_BROKER_PORT_C = atoi(cfline);
printf("Read config [MQTT_BROKER_PORT_C = %d]\n",configstruct.MQTT_BROKER_PORT_C);
}
else
{
configstruct.MQTT_BROKER_PORT_C = DEFAULT_MQTT_BROKER_PORT_C;
printf("No data entered. Using default: [MQTT_BROKER_PORT_C = %d]\n",configstruct.MQTT_BROKER_PORT_C);
}
break;

case 5:
if (strlen(cfline) > 1)
{
configstruct.ROVER_MQTT_QOS_C = atoi(cfline);
printf("Read config [ROVER_MQTT_QOS_C = %d]\n",configstruct.ROVER_MQTT_QOS_C);
}
else
{
configstruct.ROVER_MQTT_QOS_C = DEFAULT_ROVER_MQTT_QOS_C;
printf("No data entered. Using default: [ROVER_MQTT_QOS_C = %d]\n",configstruct.ROVER_MQTT_QOS_C);

}
break;

case 6:
if (strlen(cfline) > 1)
{
memcpy(configstruct.MQTT_USERNAME_C,cfline,strlen(cfline));
configstruct.MQTT_USERNAME_C[strlen(cfline)-1] = 0;
printf("Read config [MQTT_USERNAME_C = %s]\n",configstruct.MQTT_USERNAME_C);
}
else
{
printf("Read config [MQTT_USERNAME_C = <no-username>]\n");

}
break;

case 7:
if (strlen(cfline) > 1)
{
memcpy(configstruct.MQTT_PASSWORD_C,cfline,strlen(cfline));
configstruct.MQTT_PASSWORD_C[strlen(cfline)-1] = 0;
printf("Read config [MQTT_PASSWORD_C = %s]\n",configstruct.MQTT_PASSWORD_C);
}
else
{
printf("Read config [MQTT_PASSWORD_C = <no-password>]\n");

}
break;

case 8:
if (strlen(cfline) > 1)
{
configstruct.USE_GROOVE_SENSOR_C = atoi(cfline);
printf("Read config [USE_GROOVE_SENSOR_C = %d]\n",configstruct.USE_GROOVE_SENSOR_C);

}
else
{
configstruct.USE_GROOVE_SENSOR_C = DEFAULT_USE_GROOVE_SENSOR_C;
printf("No data entered. Using default: [USE_GROOVE_SENSOR_C = %d]\n",configstruct.USE_GROOVE_SENSOR_C);

}
break;

case 9:
if (strlen(cfline) > 1)
{
memcpy(configstruct.HONO_HTTP_HOST_C,cfline,strlen(cfline));
configstruct.HONO_HTTP_HOST_C[strlen(cfline)-1] = 0;
printf("Read config [HONO_HTTP_HOST_C = %s]\n",configstruct.HONO_HTTP_HOST_C);
}
else
{
memcpy(configstruct.HONO_HTTP_HOST_C,DEFAULT_HONO_HTTP_HOST_C,strlen(DEFAULT_HONO_HTTP_HOST_C));
configstruct.HONO_HTTP_HOST_C[strlen(DEFAULT_HONO_HTTP_HOST_C)-1] = 0;
printf("No data entered. Using default: [HONO_HTTP_HOST_C = %s]\n",configstruct.HONO_HTTP_HOST_C);
}
break;

case 10:
if (strlen(cfline) > 1)
{
configstruct.HONO_HTTP_PORT_C = atoi(cfline);
printf("Read config [HONO_HTTP_PORT_C = %d]\n",configstruct.HONO_HTTP_PORT_C);
}
else
{
configstruct.HONO_HTTP_PORT_C = DEFAULT_HONO_HTTP_PORT_C;
printf("No data entered. Using default: [HONO_HTTP_PORT_C = %d]\n",configstruct.HONO_HTTP_PORT_C);
}
break;

case 11:
if (strlen(cfline) > 1)
{
memcpy(configstruct.HONO_HTTP_TENANT_NAME_C,cfline,strlen(cfline));
configstruct.HONO_HTTP_TENANT_NAME_C[strlen(cfline)-1] = 0;
printf("Read config [HONO_HTTP_TENANT_NAME_C = %s]\n",configstruct.HONO_HTTP_TENANT_NAME_C);
}
else
{
memcpy(configstruct.HONO_HTTP_TENANT_NAME_C,DEFAULT_HONO_HTTP_TENANT_NAME_C,strlen(DEFAULT_HONO_HTTP_TENANT_NAME_C));
configstruct.HONO_HTTP_TENANT_NAME_C[strlen(DEFAULT_HONO_HTTP_TENANT_NAME_C)-1] = 0;
printf("No data entered. Using default: [HONO_HTTP_TENANT_NAME_C = %s]\n",configstruct.HONO_HTTP_TENANT_NAME_C);
}
break;

case 12:
if (strlen(cfline) > 1)
{
memcpy(configstruct.HONO_HTTP_DEVICE_ID_C,cfline,strlen(cfline));
configstruct.HONO_HTTP_DEVICE_ID_C[strlen(cfline)-1] = 0;
printf("Read config [HONO_HTTP_DEVICE_ID_C = %s]\n",configstruct.HONO_HTTP_DEVICE_ID_C);
}
else
{
memcpy(configstruct.HONO_HTTP_DEVICE_ID_C,DEFAULT_HONO_HTTP_DEVICE_ID_C,strlen(DEFAULT_HONO_HTTP_DEVICE_ID_C));
configstruct.HONO_HTTP_DEVICE_ID_C[strlen(DEFAULT_HONO_HTTP_DEVICE_ID_C)-1] = 0;
printf("No data entered. Using default: [HONO_HTTP_DEVICE_ID_C = %s]\n",configstruct.HONO_HTTP_DEVICE_ID_C);
}
break;

case 13:
if (strlen(cfline) > 1)
{
configstruct.HONO_HTTP_REGISTER_PORT_C = atoi(cfline);
printf("Read config [HONO_HTTP_REGISTER_PORT_C = %d]\n",configstruct.HONO_HTTP_REGISTER_PORT_C);
}
else
{
configstruct.HONO_HTTP_REGISTER_PORT_C = DEFAULT_HONO_HTTP_REGISTER_PORT_C;
printf("No data entered. Using default: [HONO_HTTP_REGISTER_PORT_C = %d]\n",configstruct.HONO_HTTP_REGISTER_PORT_C);

}
break;

case 14:
if (strlen(cfline) > 1)
{
memcpy(configstruct.HONO_HTTP_USERNAME_C,cfline,strlen(cfline));
configstruct.HONO_HTTP_USERNAME_C[strlen(cfline)-1] = 0;
printf("Read config [HONO_HTTP_USERNAME_C = %s]\n",configstruct.HONO_HTTP_USERNAME_C);
}
else
{
memcpy(configstruct.HONO_HTTP_USERNAME_C,DEFAULT_HONO_HTTP_USERNAME_C,strlen(DEFAULT_HONO_HTTP_USERNAME_C));
configstruct.HONO_HTTP_USERNAME_C[strlen(DEFAULT_HONO_HTTP_USERNAME_C)-1] = 0;
printf("No data entered. Using default: [HONO_HTTP_USERNAME_C = %s]\n",configstruct.HONO_HTTP_USERNAME_C);
}
break;

case 15:
if (strlen(cfline) > 1)
{
memcpy(configstruct.HONO_HTTP_PASSWORD_C,cfline,strlen(cfline));
configstruct.HONO_HTTP_PASSWORD_C[strlen(cfline)-1] = 0;
printf("Read config [HONO_HTTP_PASSWORD_C = %s]\n",configstruct.HONO_HTTP_PASSWORD_C);
}
else
{
memcpy(configstruct.HONO_HTTP_PASSWORD_C,DEFAULT_HONO_HTTP_PASSWORD_C,strlen(DEFAULT_HONO_HTTP_PASSWORD_C));
configstruct.HONO_HTTP_PASSWORD_C[strlen(DEFAULT_HONO_HTTP_PASSWORD_C)-1] = 0;
printf("No data entered. Using default: [HONO_HTTP_PASSWORD_C = %s]\n",configstruct.HONO_HTTP_PASSWORD_C);
}
break;

default:
break;
}

}
Expand All @@ -103,17 +256,40 @@ rover_config getRoverConfig(char *filename)
} // End if file
else
{
printf("Unable to read rover config file. Correct your /etc/rover.conf. Program will use default configuration.\n");
use_defaults = 1;
}

if (i<15 || use_defaults)
{
if (use_defaults)
printf("Unable to read rover config file. Correct your /etc/rover.conf. Program will use default configuration.\n");
else
printf("Corrupted config file. Not all entries are present. Correct your /etc/rover.conf. Program will use default configuration.\n");

/* Default setup */
configstruct.ROVER_IDENTITY_C = 1;
memcpy(configstruct.MQTT_BROKER_C,"127.0.0.1",strlen("127.0.0.1"));
configstruct.MQTT_BROKER_PORT_C = 1883;
configstruct.ROVER_MQTT_QOS_C = 0;
memcpy(configstruct.MQTT_USERNAME_C,"sensor1@DEFAULT_TENANT",strlen("sensor1@DEFAULT_TENANT"));
memcpy(configstruct.MQTT_PASSWORD_C,"hono-secret",strlen("hono-secret"));
configstruct.USE_GROOVE_SENSOR_C = 0;
configstruct.ROVER_IDENTITY_C = DEFAULT_ROVER_IDENTITY_C;
memcpy(configstruct.MQTT_BROKER_C,DEFAULT_MQTT_BROKER_C,strlen(DEFAULT_MQTT_BROKER_C));
configstruct.MQTT_BROKER_C[strlen(DEFAULT_MQTT_BROKER_C)] = 0;
configstruct.MQTT_BROKER_PORT_C = DEFAULT_MQTT_BROKER_PORT_C;
configstruct.ROVER_MQTT_QOS_C = DEFAULT_ROVER_MQTT_QOS_C;
memcpy(configstruct.MQTT_USERNAME_C,DEFAULT_MQTT_USERNAME_C,strlen(DEFAULT_MQTT_USERNAME_C));
configstruct.MQTT_USERNAME_C[strlen(DEFAULT_MQTT_USERNAME_C)] = 0;
memcpy(configstruct.MQTT_PASSWORD_C,DEFAULT_MQTT_PASSWORD_C,strlen(DEFAULT_MQTT_PASSWORD_C));
configstruct.MQTT_PASSWORD_C[strlen(DEFAULT_MQTT_PASSWORD_C)] = 0;
configstruct.USE_GROOVE_SENSOR_C = DEFAULT_USE_GROOVE_SENSOR_C;

memcpy(configstruct.HONO_HTTP_HOST_C,DEFAULT_HONO_HTTP_HOST_C,strlen(DEFAULT_HONO_HTTP_HOST_C));
configstruct.HONO_HTTP_HOST_C[strlen(DEFAULT_HONO_HTTP_HOST_C)] = 0;
configstruct.HONO_HTTP_PORT_C = 8080;
memcpy(configstruct.HONO_HTTP_TENANT_NAME_C,DEFAULT_HONO_HTTP_TENANT_NAME_C,strlen(DEFAULT_HONO_HTTP_TENANT_NAME_C));
configstruct.HONO_HTTP_TENANT_NAME_C[strlen(DEFAULT_HONO_HTTP_TENANT_NAME_C)] = 0;
memcpy(configstruct.HONO_HTTP_DEVICE_ID_C,DEFAULT_HONO_HTTP_DEVICE_ID_C,strlen(DEFAULT_HONO_HTTP_DEVICE_ID_C));
configstruct.HONO_HTTP_DEVICE_ID_C[strlen(DEFAULT_HONO_HTTP_DEVICE_ID_C)] = 0;
configstruct.HONO_HTTP_REGISTER_PORT_C = DEFAULT_HONO_HTTP_REGISTER_PORT_C;
memcpy(configstruct.HONO_HTTP_USERNAME_C,DEFAULT_HONO_HTTP_USERNAME_C,strlen(DEFAULT_HONO_HTTP_USERNAME_C));
configstruct.HONO_HTTP_USERNAME_C[strlen(DEFAULT_HONO_HTTP_USERNAME_C)] = 0;
memcpy(configstruct.HONO_HTTP_PASSWORD_C,DEFAULT_HONO_HTTP_PASSWORD_C,strlen(DEFAULT_HONO_HTTP_PASSWORD_C));
configstruct.HONO_HTTP_USERNAME_C[strlen(DEFAULT_HONO_HTTP_PASSWORD_C)] = 0;
printf ("Using following default configuration:\n");
printf("[ROVER_IDENTITY_C = %d]\n",configstruct.ROVER_IDENTITY_C);
printf("[MQTT_BROKER_C = %s]\n",configstruct.MQTT_BROKER_C);
Expand All @@ -122,6 +298,14 @@ rover_config getRoverConfig(char *filename)
printf("[MQTT_USERNAME_C = %s]\n",configstruct.MQTT_USERNAME_C);
printf("[MQTT_PASSWORD_C = %s]\n",configstruct.MQTT_PASSWORD_C);
printf("[USE_GROOVE_SENSOR_C = %d]\n",configstruct.USE_GROOVE_SENSOR_C);

printf("[HONO_HTTP_HOST_C = %s]\n",configstruct.HONO_HTTP_HOST_C);
printf("[HONO_HTTP_PORT_C = %d]\n",configstruct.HONO_HTTP_PORT_C);
printf("[HONO_HTTP_TENANT_NAME_C = %s]\n",configstruct.HONO_HTTP_TENANT_NAME_C);
printf("[HONO_HTTP_DEVICE_ID_C = %s]\n",configstruct.HONO_HTTP_DEVICE_ID_C);
printf("[HONO_HTTP_REGISTER_PORT_C = %d]\n",configstruct.HONO_HTTP_REGISTER_PORT_C);
printf("[HONO_HTTP_USERNAME_C = %s]\n",configstruct.HONO_HTTP_USERNAME_C);
printf("[HONO_HTTP_PASSWORD_C = %s]\n",configstruct.HONO_HTTP_PASSWORD_C);
}

return configstruct;
Expand Down
Loading

0 comments on commit a9d938b

Please sign in to comment.