/
gpsd.c
357 lines (263 loc) · 6.79 KB
/
gpsd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <errno.h>
#include <getopt.h>
#include <signal.h>
#include "lib/log.h"
#include "frames.h"
#include "mydb.h"
int verboseLevel= 1;
bool keepAsDaemon=true;
int timeLapse=5;
bool terminate = false;
const char *TAG="GPSd";
void help() {
printf("gpsd v1.0 Daemon de recepcion de tramas\n\n"
"Syntaxis:\n"
"\n\tgpsd [ -h | --help ] [-l | --lapse] [ -v | --verbose < 0 | 1 | 2 > ] \n"
"\n\twhere:\n"
"\t -h | --help : Shows this help.\n"
"\t -v | --verbose : Sets the verbose level( 0 Error , 1 Notice, 2 Debug )\n"
);
}
void parseArgs(int argc, char **argv) {
bool noArgs = true;
static struct option longOptions[] = {
{"help",0,0,'h'},
{"lapse",1,0,'l'},
{"verbose",1,0,'v'},
{0,0,0,0}
};
//Parseo de parametros;
int c;
while ( (c = getopt_long(argc,argv,"hl:v:n",longOptions,NULL)) != -1) {
if (noArgs)
noArgs = false;
switch(c) {
case 'l':
timeLapse = atoi(optarg);
break;
case 'v':
verboseLevel = atoi(optarg);
break;
case 'n':
keepAsDaemon = false;
break;
default:
help();
exit(EXIT_FAILURE);
}
}
if (noArgs) {
help();
exit(EXIT_FAILURE);
}
}
void initLogs() {
INITLOG(TAG);
// Para desactivar niveles de logs
if (verboseLevel==0)
LOG_LEVEL(LOG_ERR);
else if (verboseLevel==1)
LOG_LEVEL(LOG_NOTICE);
else
LOG_LEVEL(LOG_DEBUG);
}
void die(char *msg) {
LOG_E(msg);
exit(EXIT_FAILURE);
}
void signalHandler(int signal) {
switch (signal) {
case SIGHUP:
LOG_W("Hungup signal received. Do something interesting");
//TODO: Do something interesting.
break;
case SIGTERM:
LOG_W("Teminate signal received. Quitting");
terminate = true;
//exit(EXIT_SUCCESS);
break;
}
}
int processData(RES_T *res, DB_T *dbw,DB_T *dbr) {
ROW_T *row;
int rx_id,loc_id;
frm_cmd_gps_t gps;
unsigned char *buf;
size_t len,gpsLen;
int count;
int status=2;
int ok;
if (res_isClosed(res)) {
LOG_E("Recordset is closed");
return EXIT_FAILURE;
}
loc_id = 1;
gpsLen = sizeof(gps);
while (!row_isClosed(row=res_next(res)) ) {
rx_id = atoi(row_getFieldValue(row, row_getFieldIndex(row,"id")));
len = atoi(row_getFieldValue(row, row_getFieldIndex(row,"len")));
loc_id = atoi(row_getFieldValue(row, row_getFieldIndex(row,"ns")));
buf = row_getFieldValue(row, row_getFieldIndex(row,"data"));
//FIXME La gestion de decodermanager no está funcionando...
// actualmenet solo sirve para contar cuantas posibles subtramas pudiera haber..
//
ok=frame_decodermanager_decode(buf,len,&count);
if (count>0) {
if (ok)
status = 1;
//FIXME Cambiar esto que solo esta para guardar tramas GPS y tiene que ser generico
if ( frame_decode_gps(buf,len,&gps,&gpsLen) ) {
if ( !mydb_insert_gps_subframe(dbw,rx_id,loc_id,&gps,gpsLen) ) {
status = 3;
}
}
// FIXME Megachapuzada para grabar las tramas 0x15... Niños no hagais esto en casa. Va en contra de toda logíca y raciocinio...
// es mas ver esto puede producir trastornos cerebrales severos...
if ( frame_decode_rally_gps_old(buf,len,NULL,NULL)) {
int recCount;
recCount=mydb_insert_rally_old_gps_frame(dbw,rx_id,loc_id,buf,len);
LOG_F_D("Trama 0x15 (loc_id = %d, rx_id=%d) : Decodificados %d registros",loc_id,rx_id,recCount);
if (!recCount)
status = 3;
}
} else {
status = -1;
LOG_E("No se pudo decodificar nada");
}
// if ( frame_decode_gps(buf,len,&gps,&gpsLen) ) {
//
// if ( mydb_insert_gps_subframe(dbw,rx_id,loc_id,&gps,gpsLen) )
// mydb_update_transport_frame_status(dbr,rx_id,1);
// else
// mydb_update_transport_frame_status(dbr,rx_id,3);
//
// } else {
// mydb_update_transport_frame_status(dbr,rx_id,2);
// LOG_E("No es una trama gps");
// }
mydb_update_transport_frame_status(dbr,rx_id,status);
}
row_destroy(row);
return EXIT_SUCCESS;
// int num_fields;
// MYSQL_FIELD *fields;
//
// MYSQL_ROW row;
//
// MYSQL *conWrite;
//
//
// num_fields = mysql_num_fields(res);
//
// fields = mysql_fetch_fields(res);
//
// //TODO: Leer los datos descomprimirlos y guardarlos en la BBDD
// while ((row = mysql_fetch_row(res)) != NULL ) {
//
// for(int i = 0; i < num_fields; i++) {
//
// LOG_F_D("%s = %s\t", fields[i].name, row[i]?row[i]:"NULL");
//
// }
//
// }
}
int runMonitor() {
//TODO: Necesitamos doble conexions una para la lectura y otra para la escritura
DB_T *dbr;
DB_T *dbw;
RES_T *res;
LOG_N("Inicializando monitor...");
dbr = db_create("localhost","Yii","user","entrar");
dbw = db_create("localhost","Yii","user","entrar");
db_connect(dbr);
if (!db_isConnected(dbr)) {
LOG_E(db_getError(dbr));
db_destroy(dbr);
return EXIT_FAILURE;
}
db_connect(dbw);
if (!db_isConnected(dbw)) {
LOG_E(db_getError(dbw));
db_destroy(dbw);
db_destroy(dbr);
return EXIT_FAILURE;
}
frame_decodermanager_init();
while(!terminate) {
// Sospecho que tras un tiempo la conexion con la bbdd se cierra....
if (!db_chkConnection(dbr)) {
LOG_E("Read database is closed. Trying to reconnect");
db_connect(dbr);
}
// Sospecho que tras un tiempo la conexion con la bbdd se cierra....
if (!db_chkConnection(dbw)) {
LOG_E("Write database is closed. Trying to reconnect");
db_connect(dbw);
}
LOG_D("Checking for undecoded frames...");
res = mydb_select_undecoded_transport_frames(dbr);
if (res_getRowCount(res)>0) {
LOG_F_N("Processing %d new frames..",res_getRowCount(res));
processData(res,dbw,dbr);
LOG_N("... done");
}
res_destroy(res);
// if ( mysql_query(conRead,sql) ) {
//
// LOG_E(mysql_error(conRead));
//
//
// } else {
//
// res = mysql_use_result(conRead);
//
// if (res != NULL) {
//
//
// processGpsData(res);
//
// mysql_free_result(res);
//
// }
//
//
// }
LOG_F_D("...All done. Sleeping %d",timeLapse);
sleep(timeLapse); // Wait 5 seconds;
}
db_destroy(dbr);
db_destroy(dbw);
//mysql_close(conRead);
frame_decodermanager_finish();
return EXIT_SUCCESS;
}
int main(int argc, char **argv) {
struct sigaction action;
int exitCode;
parseArgs(argc,argv);
initLogs();
if (keepAsDaemon) {
LOG_N("Trying to keep as a daemon...");
if (daemon(false,false)<0) {
LOG_E(strerror(errno));
LOG_W("... continuing daemonless");
} else {
LOG_N("... Success ");
}
}
memset(&action,0,sizeof(action));
action.sa_handler =signalHandler;
sigaction(SIGHUP,&action,NULL);
sigaction(SIGTERM,&action,NULL);
// signal(SIGHUP,signalHandler); /* catch hangup signal */
// signal(SIGTERM,signalHandler); /* catch kill signal */
exitCode = runMonitor();
return exitCode;
//return EXIT_SUCCESS;
}