Subversion Repositories heating

Rev

Rev 17 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 andreas 1
/*
2
 * Copyright (C) 2015 by Andreas Theofilu. All rights reserved!
3
 *
4
 * All rights reserved. No warranty, explicit or implicit, provided.
5
 *
6
 * NOTICE:  All information contained herein is, and remains
7
 * the property of Andreas Theofilu and his suppliers, if any.
8
 * The intellectual and technical concepts contained
9
 * herein are proprietary to Andreas Theofilu and its suppliers and
10
 * may be covered by European and Foreign Patents, patents in process,
11
 * and are protected by trade secret or copyright law.
12
 * Dissemination of this information or reproduction of this material
13
 * is strictly forbidden unless prior written permission is obtained
14
 * from Andreas Theofilu.
15
 * 
16
 * Author: Andreas Theofilu <andreas@theosys.at>
17
 */
18
#include <iostream>
19
#include <fstream>
20
#include <string>
21
#include <cstring>
22
#include <cctype>
23
#include <cstddef>
24
#include <cstdio>
25
#include <cstdlib>
26
#include <fstream>
27
#include <cerrno>
28
#include <unistd.h>
29
#include <syslog.h>
30
#include <sys/stat.h>
31
#include <sys/types.h>
32
#include <fcntl.h>
33
#include "config.h"
34
 
35
CONFIGURE Configure;
54 andreas 36
bool bcmInitialized;
3 andreas 37
 
38
using namespace std;
39
 
40
config::config()
41
{
42
	initialized = false;
43
	readConf();
44
}
45
 
46
/*
47
 * The following functions read a config file and put the
48
 * contents into a structure.
49
 */
50
void config::readConf(void)
51
{
5 andreas 52
char confFile[512], line[512];
53
char *home, *p;
54
char hv0[64], hv1[128];
55
bool gid = false;
56
ifstream cfile;
3 andreas 57
 
58
	// Initialize structure with default values
59
	strcpy(Configure.user, "nouser");
60
	strcpy(Configure.group, "nogroup");
61
	strcpy(Configure.heatconf, "/etc/heating.d/heatconf.conf");
62
	strcpy(Configure.home, "/var/lib/heating/heating.db");
63
	strcpy(Configure.pidfile, "/var/run/heating.run");
5 andreas 64
	strcpy(Configure.serial, "/dev/ttyAMA0");
3 andreas 65
	Configure.port = 11006;
17 andreas 66
	Configure.html_port = 8080;
3 andreas 67
	Configure.debug = true;
68
	enableDebug(Configure.debug);
69
	// Try to find a config file
70
	home = getenv("HOME");
5 andreas 71
 
3 andreas 72
	if (!access("/etc/heating.conf",R_OK))
73
		strcpy(confFile,"/etc/heating.conf");
74
	else if (!access("/etc/heating/heating.conf",R_OK))
75
		strcpy(confFile,"/etc/heating/heating.conf");
76
	else if (!access("/usr/etc/heating.conf",R_OK))
77
		strcpy(confFile,"/usr/etc/heating.conf");
78
	else if (home)
79
	{
80
		sprintf(confFile, "%s/.heating", home);
5 andreas 81
 
3 andreas 82
		if (access(confFile,R_OK))
83
			confFile[0] = 0;
84
	}
85
	else
86
	{
87
		confFile[0] = 0;
88
		syslog(LOG_WARNING, "No config file was found!");
89
		initialized = false;
90
		return;
91
	}
5 andreas 92
 
3 andreas 93
	// If there's no config file, inform the user about it.
94
	cfile.open(confFile, ios::in);
5 andreas 95
 
3 andreas 96
	if ((cfile.rdstate() & std::ifstream::failbit) != 0)
97
	{
98
		syslog(LOG_WARNING,"Error opening the config file: %s", strerror(errno));
99
		initialized = false;
100
		return;
101
	}
102
 
103
	syslog(LOG_INFO, "Found config file %s.", confFile);
104
	// Here we've found a config file. We'll read it and set the
105
	// contents to the structure
106
	while (cfile.getline(&line[0], sizeof(line)))
107
	{
108
		int len;
109
 
110
		trim (&line[0]);
111
 
112
		if (line[0] == '#' || !char_traits<char>::length(line))
113
			continue;
114
 
115
		if ((p = findc(line, char_traits<char>::length(line), '=')) == NULL)
116
			continue;
117
 
118
		*p = 0;
119
		p++;
120
		len = char_traits<char>::length(line);
121
 
122
		if (len > (int)sizeof(hv0))
123
			len = (int)sizeof(hv0) - 1;
124
 
125
		strncpy(hv0, line, len);
126
		hv0[len] = 0;
127
		trim (hv0);
128
		len = char_traits<char>::length(p);
129
 
130
		if (len > (int)sizeof(hv1))
131
			len = (int)sizeof(hv0) - 1;
132
 
133
		strncpy(hv1, p, len);
134
		hv1[len] = 0;
135
		trim (hv1);
136
 
137
		if (!compcase(hv0, "port"))
138
			Configure.port = atoi (hv1);
139
 
140
		if (!compcase(hv0, "pidfile"))
141
			strncpy(Configure.pidfile, hv1, sizeof(Configure.pidfile));
142
 
143
		if (!compcase(hv0, "User"))
144
			strncpy (Configure.user, hv1, sizeof(Configure.user));
145
 
146
		if (!compcase(hv0, "Group"))
147
			strncpy (Configure.group, hv1, sizeof(Configure.group));
148
 
17 andreas 149
		if (!compcase(hv0, "HTMLport"))
150
			Configure.html_port = atoi(hv1);
151
 
3 andreas 152
		if (!compcase(hv0, "HeatConf"))
153
			strncpy(Configure.heatconf, hv1, sizeof(Configure.heatconf));
154
 
155
		if (!compcase(hv0, "Home"))
156
			strncpy(Configure.home, hv1, sizeof(Configure.home));
157
 
5 andreas 158
		if (!compcase(hv0, "Serial"))
159
			strncpy(Configure.serial, hv1, sizeof(Configure.serial));
160
 
3 andreas 161
		if (!compcase(hv0, "debug"))
162
		{
163
			Configure.debug = atoi (hv1);
164
			enableDebug(Configure.debug);
165
		}
166
	}
5 andreas 167
 
3 andreas 168
	cfile.close ();
169
	initialized = true;
170
}
171
 
172
CONFIGURE config::getConfig()
173
{
174
	return Configure;
175
}