02f9e3609091646125f608aee7e708406d800a28
[olsrd.git] / lib / pud / src / posFile.c
1 #include "posFile.h"
2
3 /* Plugin includes */
4 #include "pud.h"
5 #include "configTools.h"
6
7 /* OLSR includes */
8
9 /* System includes */
10 #include <stdio.h>
11 #include <string.h>
12 #include <regex.h>
13 #include <sys/stat.h>
14
15 /** the maximal length of a line that is read from the file */
16 #define LINE_LENGTH 256
17
18 /** regular expression describing a comment */
19 static const char * regexCommentString = "^([[:space:]]*|[[:space:]#]+.*)$";
20
21 /** regular expression describing a key/value pair */
22 static const char * regexNameValueString =
23                 "^[[:space:]]*([^[:space:]]+)[[:space:]]*=[[:space:]]*([^[:space:]]+)[[:space:]]*$";
24
25 /** the number of matches in regexNameValueString */
26 static const size_t regexNameValuematchCount = 3;
27
28 /** the compiled regular expression describing a comment */
29 static regex_t regexComment;
30
31 /** the compiled regular expression describing a key/value pair */
32 static regex_t regexNameValue;
33
34 /** true when the plugin has been started */
35 static bool started = false;
36
37 /** type to hold the cached stat result */
38 typedef struct _CachedStat {
39         struct timespec st_mtim; /* Time of last modification.  */
40 } CachedStat;
41
42 /** the cached stat result */
43 static CachedStat cachedStat;
44
45 /**
46  * Initialises the positionFile reader.
47  * @return true upon success, false otherwise
48  */
49 bool startPositionFile(void) {
50         if (started) {
51                 return true;
52         }
53
54         if (regcomp(&regexComment, regexCommentString, REG_EXTENDED)) {
55                 pudError(false, "Could not compile regex \"%s\"", regexCommentString);
56                 return false;
57         }
58
59         if (regcomp(&regexNameValue, regexNameValueString, REG_EXTENDED)) {
60                 pudError(false, "Could not compile regex \"%s\"", regexNameValueString);
61                 regfree(&regexComment);
62                 return false;
63         }
64
65         cachedStat.st_mtim.tv_sec = -1;
66         cachedStat.st_mtim.tv_nsec = -1;
67
68         started = true;
69         return true;
70 }
71
72 /**
73  * Cleans up the positionFile reader.
74  */
75 void stopPositionFile(void) {
76         if (started) {
77                 regfree(&regexNameValue);
78                 regfree(&regexComment);
79                 started = false;
80         }
81 }
82
83 /**
84  * Performs a regex match
85  * @param regex the compiled regex to match against
86  * @param line the line to match
87  * @param nmatch the number of matches to produce
88  * @param pmatch the array with match information
89  * @return true upon success, false otherwise
90  */
91 static bool regexMatch(regex_t * regex, char * line, size_t nmatch, regmatch_t pmatch[]) {
92         int result = regexec(regex, line, nmatch, pmatch, 0);
93         if (!result) {
94                 return true;
95         }
96
97         if (result == REG_NOMATCH) {
98                 return false;
99         }
100
101         {
102                 char msgbuf[256];
103                 regerror(result, regex, msgbuf, sizeof(msgbuf));
104                 pudError(false, "Regex match failed: %s", msgbuf);
105         }
106
107         return false;
108 }
109
110 /** the buffer in which to store a line read from the file */
111 static char line[LINE_LENGTH];
112
113 /**
114  * Read the position file
115  * @param fileName the filename
116  */
117 bool readPositionFile(char * fileName, nmeaINFO * nmeaInfo) {
118         bool retval = false;
119         struct stat statBuf;
120         nmeaINFO result;
121         FILE * fd = NULL;
122         unsigned int lineNumber = 0;
123         char * name = NULL;
124         char * value = NULL;
125
126         if (stat(fileName, &statBuf)) {
127                 /* could not access the file */
128                 goto out;
129         }
130
131         if (!memcmp(&cachedStat.st_mtim, &statBuf.st_mtim, sizeof(cachedStat.st_mtim))) {
132                 /* file did not change since last read */
133                 goto out;
134         }
135
136         fd = fopen(fileName, "r");
137         if (!fd) {
138                 goto out;
139         }
140
141         nmea_zero_INFO(&result);
142         result.sig = POSFILE_DEFAULT_SIG;
143         result.fix = POSFILE_DEFAULT_FIX;
144         result.HDOP = POSFILE_DEFAULT_HDOP;
145         result.VDOP = POSFILE_CALCULATED_VDOP(result.HDOP);
146         result.PDOP = POSFILE_CALCULATED_PDOP(result.HDOP);
147         result.lat = POSFILE_DEFAULT_LAT;
148         result.lon = POSFILE_DEFAULT_LON;
149         result.elv = POSFILE_DEFAULT_ELV;
150         result.speed = POSFILE_DEFAULT_SPEED;
151         result.direction = POSFILE_DEFAULT_DIRECTION;
152
153         memcpy(&cachedStat.st_mtim, &statBuf.st_mtim, sizeof(cachedStat.st_mtim));
154
155         while (fgets(line, LINE_LENGTH, fd)) {
156                 regmatch_t pmatch[regexNameValuematchCount];
157
158                 lineNumber++;
159
160                 if (regexMatch(&regexComment, line, 0, NULL)) {
161                         continue;
162                 }
163
164                 if (!regexMatch(&regexNameValue, line, regexNameValuematchCount, pmatch)) {
165                         pudError(false, "Position file \"%s\", line %d uses invalid syntax: %s", fileName, lineNumber, line);
166                         goto out;
167                 }
168
169                 /* determine name/value */
170                 name = &line[pmatch[1].rm_so];
171                 line[pmatch[1].rm_eo] = '\0';
172                 value = &line[pmatch[2].rm_so];
173                 line[pmatch[2].rm_eo] = '\0';
174
175                 if (!strncasecmp(POSFILE_NAME_SIG, name, sizeof(line))) {
176                         if (!strncasecmp(POSFILE_VALUE_SIG_BAD, value, sizeof(line))) {
177                                 result.sig = NMEA_SIG_BAD;
178                         } else if (!strncasecmp(POSFILE_VALUE_SIG_LOW, value, sizeof(line))) {
179                                 result.sig = NMEA_SIG_LOW;
180                         } else if (!strncasecmp(POSFILE_VALUE_SIG_MID, value, sizeof(line))) {
181                                 result.sig = NMEA_SIG_MID;
182                         } else if (!strncasecmp(POSFILE_VALUE_SIG_HIGH, value, sizeof(line))) {
183                                 result.sig = NMEA_SIG_HIGH;
184                         } else {
185                                 pudError(false, "Position file \"%s\", line %d uses an invalid value for \"%s\","
186                                                 " valid values are [%s|%s\%s|%s]", fileName, lineNumber, POSFILE_NAME_SIG,
187                                                 POSFILE_VALUE_SIG_BAD, POSFILE_VALUE_SIG_LOW, POSFILE_VALUE_SIG_MID, POSFILE_VALUE_SIG_HIGH);
188                                 goto out;
189                         }
190                 } else if (!strncasecmp(POSFILE_NAME_FIX, name, sizeof(line))) {
191                         if (!strncasecmp(POSFILE_VALUE_FIX_BAD, value, sizeof(line))) {
192                                 result.fix = NMEA_FIX_BAD;
193                         } else if (!strncasecmp(POSFILE_VALUE_FIX_2D, value, sizeof(line))) {
194                                 result.fix = NMEA_FIX_2D;
195                         } else if (!strncasecmp(POSFILE_VALUE_FIX_3D, value, sizeof(line))) {
196                                 result.fix = NMEA_FIX_3D;
197                         } else {
198                                 pudError(false, "Position file \"%s\", line %d uses an invalid value for \"%s\","
199                                                 " valid values are [%s\%s|%s]", fileName, lineNumber, POSFILE_NAME_FIX, POSFILE_VALUE_FIX_BAD,
200                                                 POSFILE_VALUE_FIX_2D, POSFILE_VALUE_FIX_3D);
201                                 goto out;
202                         }
203                 } else if (!strncasecmp(POSFILE_NAME_HDOP, name, sizeof(line))) {
204                         double val;
205                         if (!readDouble(POSFILE_NAME_HDOP, value, &val)) {
206                                 goto out;
207                         }
208
209                         result.HDOP = val;
210                         result.VDOP = POSFILE_CALCULATED_VDOP(result.HDOP);
211                         result.PDOP = POSFILE_CALCULATED_PDOP(result.HDOP);
212                 } else if (!strncasecmp(POSFILE_NAME_LAT, name, sizeof(line))) {
213                         double val;
214                         if (!readDouble(POSFILE_NAME_LAT, value, &val)) {
215                                 goto out;
216                         }
217
218                         result.lat = val;
219                 } else if (!strncasecmp(POSFILE_NAME_LON, name, sizeof(line))) {
220                         double val;
221                         if (!readDouble(POSFILE_NAME_LON, value, &val)) {
222                                 goto out;
223                         }
224
225                         result.lon = val;
226                 } else if (!strncasecmp(POSFILE_NAME_ELV, name, sizeof(line))) {
227                         double val;
228                         if (!readDouble(POSFILE_NAME_ELV, value, &val)) {
229                                 goto out;
230                         }
231
232                         result.elv = val;
233                 } else if (!strncasecmp(POSFILE_NAME_SPEED, name, sizeof(line))) {
234                         double val;
235                         if (!readDouble(POSFILE_NAME_SPEED, value, &val)) {
236                                 goto out;
237                         }
238
239                         result.speed = val;
240                 } else if (!strncasecmp(POSFILE_NAME_DIRECTION, name, sizeof(line))) {
241                         double val;
242                         if (!readDouble(POSFILE_NAME_DIRECTION, value, &val)) {
243                                 goto out;
244                         }
245
246                         result.direction = val;
247                 } else {
248                         pudError(false, "Position file \"%s\", line %d uses an invalid option \"%s\","
249                                         " valid options are [%s|%s|%s|%s|%s|%s|%s|%s]", fileName, lineNumber, name, POSFILE_NAME_SIG,
250                                         POSFILE_NAME_FIX, POSFILE_NAME_HDOP, POSFILE_NAME_LAT, POSFILE_NAME_LON, POSFILE_NAME_ELV,
251                                         POSFILE_NAME_SPEED, POSFILE_NAME_DIRECTION);
252                         goto out;
253                 }
254         }
255
256         fclose(fd);
257
258         result.smask = POSFILE_SANITISE_SMASK;
259         nmea_INFO_sanitise(&result);
260         nmea_INFO_unit_conversion(&result);
261         if (result.fix == NMEA_FIX_BAD) {
262                 result.smask = 0;
263         } else {
264                 result.smask = POSFILE_DEFAULT_SMASK;
265         }
266
267         memcpy(nmeaInfo, &result, sizeof(result));
268         retval = true;
269
270         out: return retval;
271 }