27
27
#include <sys/types.h>
28
28
#include <fcntl.h>
29
29
#include <err.h>
30
+ #include <stdbool.h>
30
31
31
32
#include "dlt.h"
32
33
#include "dlt-qnx-system.h"
@@ -36,18 +37,30 @@ DLT_DECLARE_CONTEXT(dltQnxSystem)
36
37
/* Global variables */
37
38
volatile DltQnxSystemThreads g_threads ;
38
39
40
+ #define INJECTION_SLOG2_ADAPTER 4096
41
+
42
+ #define DATA_DISABLED "00"
43
+ #define DATA_ENABLED "01"
44
+
45
+ volatile bool g_inj_disable_slog2_cb = false;
46
+
39
47
/* Function prototype */
40
48
static void daemonize ();
41
- static void start_threads (DltQnxSystemConfiguration * config );
49
+ static void start_threads ();
42
50
static void join_threads ();
43
- static int read_configuration_file (DltQnxSystemConfiguration * config ,
44
- const char * file_name );
51
+ static int read_configuration_file (const char * file_name );
45
52
static int read_command_line (DltQnxSystemCliOptions * options , int argc , char * argv []);
46
53
54
+ static int dlt_injection_cb (uint32_t service_id , void * data , uint32_t length );
55
+
56
+ static DltQnxSystemConfiguration * g_dlt_qnx_conf = NULL ;
57
+ static void init_configuration ();
58
+ static void clean_up ();
59
+
60
+
47
61
int main (int argc , char * argv [])
48
62
{
49
63
DltQnxSystemCliOptions options ;
50
- DltQnxSystemConfiguration config ;
51
64
int sigNo = 0 ;
52
65
int ret = 0 ;
53
66
sigset_t mask ;
@@ -59,7 +72,7 @@ int main(int argc, char* argv[])
59
72
return -1 ;
60
73
}
61
74
62
- if (read_configuration_file (& config , options .configurationFileName ) < 0 )
75
+ if (read_configuration_file (options .configurationFileName ) < 0 )
63
76
{
64
77
fprintf (stderr , "Failed to read configuration file!\n" );
65
78
return -1 ;
@@ -82,17 +95,19 @@ int main(int argc, char* argv[])
82
95
return -1 ;
83
96
}
84
97
85
- DLT_REGISTER_APP (config . applicationId , "DLT QNX System" );
86
- DLT_REGISTER_CONTEXT (dltQnxSystem , config . applicationContextId ,
98
+ DLT_REGISTER_APP (g_dlt_qnx_conf -> applicationId , "DLT QNX System" );
99
+ DLT_REGISTER_CONTEXT (dltQnxSystem , g_dlt_qnx_conf -> applicationContextId ,
87
100
"Context of main dlt qnx system manager" );
101
+ dlt_register_injection_callback (& dltQnxSystem ,
102
+ INJECTION_SLOG2_ADAPTER , dlt_injection_cb );
88
103
89
104
DLT_LOG (dltQnxSystem , DLT_LOG_DEBUG ,
90
105
DLT_STRING ("Setting signals wait for abnormal exit" ));
91
106
92
107
g_threads .mainThread = pthread_self ();
93
108
94
109
DLT_LOG (dltQnxSystem , DLT_LOG_DEBUG , DLT_STRING ("Launching threads." ));
95
- start_threads (& config );
110
+ start_threads ();
96
111
97
112
ret = sigwait (& mask , & sigNo );
98
113
@@ -114,6 +129,7 @@ int main(int argc, char* argv[])
114
129
DLT_STRING (strsignal (sigNo )));
115
130
116
131
DLT_UNREGISTER_APP_FLUSH_BUFFERED_LOGS ();
132
+ clean_up ();
117
133
return 0 ;
118
134
119
135
}
@@ -193,23 +209,22 @@ static int read_command_line(DltQnxSystemCliOptions *options, int argc, char *ar
193
209
/**
194
210
* Initialize configuration to default values.
195
211
*/
196
- static void init_configuration (DltQnxSystemConfiguration * config )
212
+ static void init_configuration ()
197
213
{
214
+ g_dlt_qnx_conf = calloc (1 , sizeof (DltQnxSystemConfiguration ));
198
215
/* Common */
199
- config -> applicationId = "QSYM" ;
200
- config -> applicationContextId = "QSYC" ;
201
-
216
+ g_dlt_qnx_conf -> applicationId = strdup ("QSYM" );
217
+ g_dlt_qnx_conf -> applicationContextId = strdup ("QSYC" );
202
218
/* Slogger2 */
203
- config -> qnxslogger2 .enable = 0 ;
204
- config -> qnxslogger2 .contextId = "QSLA" ;
205
- config -> qnxslogger2 .useOriginalTimestamp = 1 ;
219
+ g_dlt_qnx_conf -> qnxslogger2 .enable = 0 ;
220
+ g_dlt_qnx_conf -> qnxslogger2 .contextId = strdup ( "QSLA" ) ;
221
+ g_dlt_qnx_conf -> qnxslogger2 .useOriginalTimestamp = 1 ;
206
222
}
207
223
208
224
/**
209
225
* Read options from the configuration file
210
226
*/
211
- static int read_configuration_file (DltQnxSystemConfiguration * config ,
212
- const char * file_name )
227
+ static int read_configuration_file (const char * file_name )
213
228
{
214
229
FILE * file ;
215
230
char * line ;
@@ -218,12 +233,16 @@ static int read_configuration_file(DltQnxSystemConfiguration *config,
218
233
char * pch ;
219
234
int ret = 0 ;
220
235
221
- init_configuration (config );
236
+ init_configuration ();
237
+ if (g_dlt_qnx_conf == NULL ) {
238
+ fprintf (stderr ,
239
+ "dlt-qnx-system, could not allocate memory.\n" );
240
+ return -1 ;
241
+ }
222
242
223
243
file = fopen (file_name , "r" );
224
244
225
- if (file == NULL )
226
- {
245
+ if (file == NULL ) {
227
246
fprintf (stderr ,
228
247
"dlt-qnx-system, could not open configuration file.\n" );
229
248
return -1 ;
@@ -270,42 +289,34 @@ static int read_configuration_file(DltQnxSystemConfiguration *config,
270
289
/* Common */
271
290
if (strcmp (token , "ApplicationId" ) == 0 )
272
291
{
273
- config -> applicationId = (char * )malloc (strlen (value ) + 1 );
274
- MALLOC_ASSERT (config -> applicationId );
275
- /**
276
- * strcpy unritical here, because size matches exactly the
277
- * size to be copied
278
- */
279
- strcpy (config -> applicationId , value );
292
+ if (g_dlt_qnx_conf -> applicationId )
293
+ free (g_dlt_qnx_conf -> applicationId );
294
+ g_dlt_qnx_conf -> applicationId = strndup (value , DLT_ID_SIZE );
295
+ MALLOC_ASSERT (g_dlt_qnx_conf -> applicationId );
280
296
}
281
297
else if (strcmp (token , "ApplicationContextID" ) == 0 )
282
298
{
283
- config -> applicationContextId = (char * )malloc (strlen (value ) + 1 );
284
- MALLOC_ASSERT (config -> applicationContextId );
285
- /**
286
- * strcpy unritical here, because size matches exactly
287
- * the size to be copied
288
- */
289
- strcpy (config -> applicationContextId , value );
299
+ if (g_dlt_qnx_conf -> applicationContextId )
300
+ free (g_dlt_qnx_conf -> applicationContextId );
301
+ g_dlt_qnx_conf -> applicationContextId = strndup (value , DLT_ID_SIZE );
302
+ MALLOC_ASSERT (g_dlt_qnx_conf -> applicationContextId );
303
+ strncpy (g_dlt_qnx_conf -> applicationContextId , value , DLT_ID_SIZE );
290
304
}
291
305
/* Slogger2 */
292
306
else if (strcmp (token , "QnxSlogger2Enable" ) == 0 )
293
307
{
294
- config -> qnxslogger2 .enable = atoi (value );
308
+ g_dlt_qnx_conf -> qnxslogger2 .enable = atoi (value );
295
309
}
296
310
else if (strcmp (token , "QnxSlogger2ContextId" ) == 0 )
297
311
{
298
- config -> qnxslogger2 .contextId = (char * )malloc (strlen (value ) + 1 );
299
- MALLOC_ASSERT (config -> qnxslogger2 .contextId );
300
- /**
301
- * strcpy unritical here, because size matches exactly
302
- * the size to be copied
303
- */
304
- strcpy (config -> qnxslogger2 .contextId , value );
312
+ if (g_dlt_qnx_conf -> qnxslogger2 .contextId )
313
+ free (g_dlt_qnx_conf -> qnxslogger2 .contextId );
314
+ g_dlt_qnx_conf -> qnxslogger2 .contextId = strndup (value , DLT_ID_SIZE );
315
+ MALLOC_ASSERT (g_dlt_qnx_conf -> qnxslogger2 .contextId );
305
316
}
306
317
else if (strcmp (token , "QnxSlogger2UseOriginalTimestamp" ) == 0 )
307
318
{
308
- config -> qnxslogger2 .useOriginalTimestamp = atoi (value );
319
+ g_dlt_qnx_conf -> qnxslogger2 .useOriginalTimestamp = atoi (value );
309
320
}
310
321
else
311
322
{
@@ -372,16 +383,10 @@ static void daemonize()
372
383
signal (SIGTTIN , SIG_IGN );
373
384
}
374
385
375
- static void start_threads (DltQnxSystemConfiguration * config )
386
+ static void start_threads ()
376
387
{
377
388
int i = 0 ;
378
389
379
- /* Check parameter */
380
- if (!config )
381
- {
382
- return ;
383
- }
384
-
385
390
DLT_LOG (dltQnxSystem , DLT_LOG_DEBUG ,
386
391
DLT_STRING ("dlt-qnx-system, start threads" ));
387
392
@@ -393,9 +398,9 @@ static void start_threads(DltQnxSystemConfiguration *config)
393
398
g_threads .threads [i ] = 0 ;
394
399
}
395
400
396
- if (config -> qnxslogger2 .enable )
401
+ if (g_dlt_qnx_conf -> qnxslogger2 .enable )
397
402
{
398
- start_qnx_slogger2 (config );
403
+ start_qnx_slogger2 (g_dlt_qnx_conf );
399
404
}
400
405
}
401
406
@@ -430,3 +435,45 @@ static void join_threads()
430
435
431
436
DLT_UNREGISTER_CONTEXT (dltQnxSystem );
432
437
}
438
+
439
+ static int dlt_injection_cb (uint32_t service_id , void * data , uint32_t length )
440
+ {
441
+ (void ) length ;
442
+ DLT_LOG (dltQnxSystem , DLT_LOG_INFO ,
443
+ DLT_STRING ("Injection received:" ),
444
+ DLT_INT32 (service_id ));
445
+
446
+ if (service_id != INJECTION_SLOG2_ADAPTER )
447
+ return -1 ;
448
+
449
+ if (0 == strncmp ((char * ) data , DATA_DISABLED , sizeof (DATA_DISABLED )- 1 ))
450
+ g_inj_disable_slog2_cb = true;
451
+ else if (0 == strncmp ((char * ) data , DATA_ENABLED , sizeof (DATA_ENABLED )- 1 )) {
452
+ if (g_inj_disable_slog2_cb == true) {
453
+ g_inj_disable_slog2_cb = false;
454
+ start_threads ();
455
+ }
456
+ }
457
+
458
+ return 0 ;
459
+ }
460
+
461
+ static void clean_up ()
462
+ {
463
+ if (g_dlt_qnx_conf -> applicationId ) {
464
+ free (g_dlt_qnx_conf -> applicationId );
465
+ g_dlt_qnx_conf -> applicationId = NULL ;
466
+ }
467
+ if (g_dlt_qnx_conf -> applicationContextId ) {
468
+ free (g_dlt_qnx_conf -> applicationContextId );
469
+ g_dlt_qnx_conf -> applicationContextId = NULL ;
470
+ }
471
+ if (g_dlt_qnx_conf -> qnxslogger2 .contextId ) {
472
+ free (g_dlt_qnx_conf -> qnxslogger2 .contextId );
473
+ g_dlt_qnx_conf -> qnxslogger2 .contextId = NULL ;
474
+ }
475
+ if (g_dlt_qnx_conf ) {
476
+ free (g_dlt_qnx_conf );
477
+ g_dlt_qnx_conf = NULL ;
478
+ }
479
+ }
0 commit comments