summaryrefslogtreecommitdiff
path: root/logger/test/log-gnss-sns.cpp
blob: bb2561faa45c51151ed2534df655b1911db35a73 (plain)
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/**************************************************************************
* @licence app begin@
*
* SPDX-License-Identifier: MPL-2.0
*
* \brief Test program for GNSS+SNS logging
*
*
* \author Helmut Schmidt <https://github.com/huirad>
*
* \copyright Copyright (C) 2015, Helmut Schmidt
*
* \license
* This Source Code Form is subject to the terms of the
* Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with
* this file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* @licence end@
**************************************************************************/


#define __STDC_FORMAT_MACROS
#include <inttypes.h>


#include "poslog.h"
#include "gnsslog.h"
#include "snslog.h"
#if (DLT_ENABLED)
#include "dlt.h"
#endif

#include <syslog.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <pthread.h>
#include <signal.h>
#include <inttypes.h>

#include "gnss-init.h"
#include "gnss.h"
#include "gnss-status.h"
#include "sns-init.h"



/**
 * Double buffer for log strings
 * Purpose: avoid blocking of callback functions by I/O
 * Multiple writer threads are allowed but only on reader thread
 */
class DBuf {

#define DBUF_STRING_SIZE 256
#define DBUF_NUM_LINES 512

    struct SBuf {
    public:
        char strings [DBUF_STRING_SIZE] [DBUF_NUM_LINES];
        uint16_t rnext;
        uint16_t wnext;
        SBuf(): rnext(0), wnext(0) {};
    };

    SBuf* wbuf;
    SBuf* rbuf;
    pthread_mutex_t wmutex;
    pthread_mutex_t rmutex;
public:

    DBuf()
    {
        pthread_mutex_init(&wmutex, NULL);
        pthread_mutex_init(&rmutex, NULL);
        wbuf = new(SBuf);
        rbuf = new(SBuf);
    }

    /**
     * Add a string to the buffer.
     * Return true on success, false on failure (e.g. buffer full).
     */
    bool write(const char* s)
    {
        bool retval = false;
        pthread_mutex_lock(&wmutex);
        if (s && wbuf && (wbuf->wnext < DBUF_NUM_LINES))
        {
            strncpy(wbuf->strings[wbuf->wnext], s, DBUF_STRING_SIZE-1);
            wbuf->strings[wbuf->wnext][DBUF_STRING_SIZE-1] = 0;
            wbuf->wnext++;
            retval = true;
        }
        pthread_mutex_unlock(&wmutex);
        return retval;
    }

    /**
     * Read next string from the buffer
     * Return NULL, if no more string available
     */
    const char* read()
    {
        const char* ret = NULL;
        pthread_mutex_lock(&rmutex);
        if (rbuf && (rbuf->rnext < rbuf->wnext) && (rbuf->rnext < DBUF_NUM_LINES) )
        {
            ret = rbuf->strings[rbuf->rnext];
            rbuf->rnext++;
        }
        pthread_mutex_unlock(&rmutex);
        return ret;
    }

    /**
     * Swap read and write buffers.
     * Clears read buffer before to ensure that new write buffer is empty.
     * Shall only be called by reader thread when read buffer has been
     * completely processed.
     */
    void swap()
    {
        SBuf* tmp;
        pthread_mutex_lock(&rmutex);
        rbuf->rnext = 0;
        rbuf->wnext = 0;
        pthread_mutex_lock(&wmutex);
        tmp = wbuf;
        wbuf = rbuf;
        rbuf = tmp;
        pthread_mutex_unlock(&wmutex);
        pthread_mutex_unlock(&rmutex);
    }

};



#define GNSS_INIT_MAX_RETRIES 30

//global variable to control the main loop - will be set by signal handlers or status callback
enum EExitCondition {
    eExitNone = 0,
    eExitSigInt,
    eExitGnssFailure
};

static volatile bool g_sigterm = false;
static volatile EExitCondition g_exit = eExitNone;
static volatile bool g_gnss_failure = false;
//global pointer to the double buffer
DBuf* g_dbuf = 0;
pthread_t g_logthread;
uint32_t g_write_failures = 0;
FILE* g_logfile = 0;

/**
 * Logger callback to add string to the double buffer.
 */
void dbufCb(const char* string)
{
    if (g_dbuf)
    {
        bool write_success = g_dbuf->write(string);
        if (!write_success)
        {
            g_write_failures++;
        }
        //printf("WBUF %s\n", string);
    }
}

/**
 * Background thread to write double buffer to a file.
 *
 */
void* loop_log_writer(void*)
{
    bool stop = false;
    //printf("LOGWRITER\n");
    while (g_dbuf && !stop && !g_sigterm)
    {
        const char* s = 0;
        //process read buffer - should always be empty
        while (s = g_dbuf->read())
        {
            printf("BUF1 %s\n", s);
        }
        //swap and process previous write buffer
        g_dbuf->swap();
        while (s = g_dbuf->read())
        {
            fprintf(g_logfile, "%s\n", s);
        }
        fflush(g_logfile);
        //printf("LOGWRITER SLEEP\n");
        if (g_exit == eExitNone)
        {
            sleep(2);
            //TODO it seems that only the main thread will be interrupted by the signal???
            //TODO CHECK how to force faster reaction
        }
        else
        {
            stop = true;
        }
        //printf("LOGWRITER WAKEUP\n");
    }
    //printf("LOGWRITER END\n");
}

static void sigHandler (int sig, siginfo_t *siginfo, void *context)
{
    if (sig == SIGINT) 
    {
        g_exit = eExitSigInt;
        if (g_logfile)
        {
            //ensure that also the logger thread is interrupted
            //pthread_kill(g_logthread, sig);  //seems not to work somehow
        }
    }
    else 
    if (sig == SIGTERM) 
    {
        g_sigterm = true;
    }
}

static bool registerSigHandlers()
{
    bool is_success = true;
    
    struct sigaction action;
    memset (&action, '\0', sizeof(action));
    action.sa_sigaction = &sigHandler;
    action.sa_flags = SA_SIGINFO;

    if (sigaction(SIGINT, &action, NULL) < 0)
    {
        is_success = false;
    }
    if (sigaction(SIGTERM, &action, NULL) < 0)
    {
        is_success = false;
    }
    return is_success;
}



static void cbTime(const TGNSSTime time[], uint16_t numElements)
{
    gnssTimeLog(gnsslogGetTimestamp(), time, numElements);
}

static void cbPosition(const TGNSSPosition position[], uint16_t numElements)
{
    gnssPositionLog(gnsslogGetTimestamp(), position, numElements);
}

static void cbGNSSStatus(const TGNSSStatus *status)
{
    if (status && (status->validityBits & GNSS_STATUS_STATUS_VALID))
    {
        char status_string[64];
        sprintf(status_string, "#GNSS Status: %d", status->status);
        poslogAddString(status_string);
        if (status->status == GNSS_STATUS_FAILURE)
        {
            g_exit = eExitGnssFailure;
        }
    }
}

static void cbAccel(const TAccelerationData accelerationData[], uint16_t numElements)
{
    accelerationDataLog(snslogGetTimestamp(), accelerationData, numElements);
}

static void cbGyro(const TGyroscopeData gyroData[], uint16_t numElements)
{
    gyroscopeDataLog(snslogGetTimestamp(), gyroData, numElements);
}

#define UDP_LOG
#ifdef UDP_LOG
#include<stdlib.h> //exit(0);
#include<arpa/inet.h>
#include<sys/socket.h>
#define UDP_BUFLEN  128 //Max length of buffer
#define UDP_PORT 5701   //The port on which to listen for incoming data
#define UDP_LOGLEN  256

pthread_t g_udpthread;

void* loop_udp_log(void*)
{
    struct sockaddr_in si_me, si_other;
    int s;
    socklen_t slen = sizeof(si_other);
    ssize_t recv_len;
    char buf[UDP_BUFLEN];
    char log_string[UDP_LOGLEN];
    bool ok = true;

    //create a UDP socket
    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    {
        ok = false;
    }

    if (ok)
    {
        // zero out the structure
        memset((char *) &si_me, 0, sizeof(si_me));
        si_me.sin_family = AF_INET;
        si_me.sin_port = htons(UDP_PORT);
        si_me.sin_addr.s_addr = htonl(INADDR_ANY);

        //bind socket to port
        if( bind(s , (struct sockaddr*)&si_me, sizeof(si_me) ) == -1)
        {
            ok = false;
        }
    }

    //keep listening for data
    while(ok)
    {
        //try to receive some data, this is a blocking call
        if ((recv_len = recvfrom(s, buf, UDP_BUFLEN-1, 0, (struct sockaddr *) &si_other, &slen)) == -1)
        {
            ok = false;
        }
        else
        {
            buf[recv_len]=0;
            snprintf(log_string, UDP_LOGLEN-1, "%"PRIu64",0,$UDP,%s,%d,%s",
                snslogGetTimestamp(),
                inet_ntoa(si_other.sin_addr),
                ntohs(si_other.sin_port),
                buf
            );
            log_string[UDP_LOGLEN-1] = 0; //ensure that string is null-terminated
            poslogAddString(log_string);
        }
    }
    close(s);
}
#endif




int main (int argc, char *argv[])
{
    int major;
    int minor;
    int micro;
    char version_string[64];

    bool is_poslog_init_ok = false;
    bool is_sns_init_ok = false;    
    bool is_sns_gyro_init_ok = false;
    bool is_sns_accel_init_ok = false;
    bool is_gnss_init_ok = false;
    int gnss_init_tries = 0;

    registerSigHandlers();

#if (DLT_ENABLED)
    DLT_REGISTER_APP("GLT","GNSS/SNS Logger");
#endif
    poslogSetFD(STDOUT_FILENO);
    is_poslog_init_ok = poslogInit();

    if (is_poslog_init_ok)
    {

        if(argv[1] && (g_logfile = fopen(argv[1], "a")))
        {
            g_dbuf = new(DBuf);
            poslogSetCB(dbufCb);
            pthread_create(&g_logthread, NULL, loop_log_writer, NULL);
            poslogSetActiveSinks(POSLOG_SINK_DLT|POSLOG_SINK_CB);
        }
        else
        {
            poslogSetActiveSinks(POSLOG_SINK_DLT|POSLOG_SINK_FD|POSLOG_SINK_CB);
        }

        gnssGetVersion(&major, &minor, &micro);
        sprintf(version_string, "0,0$GVGNSVER,%d,%d,%d", major, minor, micro);
        poslogAddString(version_string);
        snsGetVersion(&major, &minor, &micro);
        sprintf(version_string, "0,0$GVSNSVER,%d,%d,%d", major, minor, micro);
        poslogAddString(version_string);

#ifdef UDP_LOG
        pthread_create(&g_udpthread, NULL, loop_udp_log, NULL);
#endif
        is_sns_init_ok = snsInit();
        if (is_sns_init_ok)
        {
            is_sns_gyro_init_ok = snsGyroscopeInit();
            if(is_sns_gyro_init_ok)
            {
                poslogAddString("#INF snsGyroscopeInit() success");
                snsGyroscopeRegisterCallback(&cbGyro);
            }
            is_sns_accel_init_ok = snsAccelerationInit();
            if (is_sns_accel_init_ok)
            {
                poslogAddString("#INF snsAccelerationInit() success");
                snsAccelerationRegisterCallback(&cbAccel);
            }
            if (!is_sns_gyro_init_ok && !is_sns_accel_init_ok)
            {
                is_sns_init_ok = false;
                snsDestroy();
            }
            else
            {
                poslogAddString("#INF snsInit() success");
            }
        }

        //GNSS device may be available a bit late after startup
        is_gnss_init_ok = gnssInit();
        while (!is_gnss_init_ok && (gnss_init_tries < GNSS_INIT_MAX_RETRIES) && (g_exit == eExitNone) && !g_sigterm)
        {
            sleep(1);
            is_gnss_init_ok = gnssInit();
            gnss_init_tries += 1;
        }
        if (is_gnss_init_ok)
        {
            poslogAddString("#INF gnssInit() success");
            gnssRegisterTimeCallback(&cbTime);
            gnssRegisterPositionCallback(&cbPosition);
            gnssRegisterStatusCallback(&cbGNSSStatus);
        }

        if (is_sns_init_ok || is_gnss_init_ok)
        {
            while(!g_sigterm && (g_exit == eExitNone))
            {
                sleep(1);
            }
        }
        else
        {
            poslogAddString("#ERR snsInit() or gnssInit() failure - terminating");
        }
        
        //if not interrupted by SIGTERM then we have time to cleanup
        if (!g_sigterm)
        {
            if (g_exit == eExitSigInt)
            {
                poslogAddString("#SIGINT");
            }
            if (g_exit == eExitGnssFailure)
            {
                poslogAddString("#GNSS_STATUS_FAILURE");
            }
            if (is_sns_init_ok)
            {
                if (is_sns_accel_init_ok)
                {
                    snsAccelerationDeregisterCallback(&cbAccel);
                    snsAccelerationDestroy();
                }
                if (is_sns_gyro_init_ok)
                {
                    snsGyroscopeRegisterCallback(&cbGyro);
                    snsGyroscopeDestroy();
                }
                snsDestroy();
            }
            if (is_gnss_init_ok)
            {
                gnssDeregisterStatusCallback(&cbGNSSStatus);
                gnssDeregisterPositionCallback(&cbPosition);
                gnssDeregisterTimeCallback(&cbTime);
                gnssDestroy();
            }
        }
        if (g_logfile)
        {
            pthread_join(g_logthread, NULL);
            fclose(g_logfile);
            printf("#Write Failures: %"PRIu32"\n", g_write_failures);
        }
        poslogDestroy();
    }

#if (DLT_ENABLED)
    DLT_UNREGISTER_APP();
#endif
}