summaryrefslogtreecommitdiff
path: root/src/daemon/dlt_daemon_offline_logstorage.c
blob: 2d287966a7fca326fa77246e3d2e84b88a568c62 (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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
/**
 * Copyright (C) 2013 - 2018  Advanced Driver Information Technology.
 * This code is developed by Advanced Driver Information Technology.
 * Copyright of Advanced Driver Information Technology, Bosch and DENSO.
 *
 * DLT offline log storage functionality source file.
 *
 * \copyright
 * 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/.
 *
 *
 * \author Syed Hameed <shameed@jp.adit-jv.com> ADIT 2013 - 2015
 * \author Christoph Lipka <clipka@jp.adit-jv.com> ADIT 2015
 *
 * \file: dlt_daemon_offline_logstorage.c
 * For further information see http://www.genivi.org/.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>

#include "dlt_daemon_offline_logstorage.h"
#include "dlt_daemon_offline_logstorage_internal.h"
#include "dlt_gateway_types.h"
#include "dlt_gateway.h"

/**
 * dlt_logstorage_split_ecuid
 *
 * Split keys with ECU ID alone
 *
 * @param key            Key
 * @param len            Key length
 * @param ecuid          ECU ID from key stored here
 * @param appid           Application ID as .* stored here
 * @param ctxid          Context id as .* stored here
 * @return               0 on success -1 on error
 */
DLT_STATIC DltReturnValue dlt_logstorage_split_ecuid(char *key,
                                                     int len,
                                                     char *ecuid,
                                                     char *appid,
                                                     char *ctxid)
{
    if ((len > (DLT_ID_SIZE + 2)) || (len < 2))
        return DLT_RETURN_ERROR;

    strncpy(ecuid, key, (len - 2));
    strncpy(appid, ".*", 2);
    strncpy(ctxid, ".*", 2);

    return DLT_RETURN_OK;
}

/**
 * dlt_logstorage_split_ctid
 *
 * Split keys with Context ID alone
 *
 * @param key            Key
 * @param len            Key length
 * @param appid          Application ID as .* stored here
 * @param ctxid          Context id from key stored here
 * @return               0 on success -1 on error
 */
DLT_STATIC DltReturnValue dlt_logstorage_split_ctid(char *key,
                                                    int len,
                                                    char *appid,
                                                    char *ctxid)
{
    if ((len > (DLT_ID_SIZE + 2)) || (len < 1))
        return DLT_RETURN_ERROR;

    strncpy(ctxid, (key + 2), (len - 1));
    strncpy(appid, ".*", 2);

    return DLT_RETURN_OK;
}

/**
 * dlt_logstorage_split_apid
 *
 * Split keys with Application ID alone
 *
 * @param key            Key
 * @param len            Key length
 * @param appid          Application ID from key is stored here
 * @param ctxid          Context id as .* stored here
 * @return               0 on success -1 on error
 */
DLT_STATIC DltReturnValue dlt_logstorage_split_apid(char *key,
                                                    int len,
                                                    char *appid,
                                                    char *ctxid)
{
    if ((len > (DLT_ID_SIZE + 2)) || (len < 2))
        return DLT_RETURN_ERROR;

    strncpy(appid, key + 1, (len - 2));
    strncpy(ctxid, ".*", 2);

    return DLT_RETURN_OK;
}

/**
 * dlt_logstorage_split_apid_ctid
 *
 * Split keys with Application ID and Context ID
 *
 * @param key            Key
 * @param len            Key length
 * @param appid          Application ID from key is stored here
 * @param ctxid          CContext id from key is stored here
 * @return               0 on success -1 on error
 */
DLT_STATIC DltReturnValue dlt_logstorage_split_apid_ctid(char *key,
                                                         int len,
                                                         char *appid,
                                                         char *ctxid)
{
    char *tok = NULL;

    if (len > DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN)
        return DLT_RETURN_ERROR;

    /* copy appid and ctxid */
    tok = strtok(key, ":");

    if (tok != NULL)
        strncpy(appid, tok, DLT_ID_SIZE);
    else
        return DLT_RETURN_ERROR;

    tok = strtok(NULL, ":");

    if (tok != NULL)
        strncpy(ctxid, tok, DLT_ID_SIZE);
    else
        return DLT_RETURN_ERROR;

    return DLT_RETURN_OK;
}

/**
 * dlt_logstorage_split_ecuid_apid
 *
 * Split keys with ECU ID and Application ID
 *
 * @param key            Key
 * @param len            Key length
 * @param ecuid          ECU ID from key stored here
 * @param appid          Application ID from key is stored here
 * @param ctxid          CContext id as .* stored here
 * @return               0 on success -1 on error
 */
DLT_STATIC DltReturnValue dlt_logstorage_split_ecuid_apid(char *key,
                                                          int len,
                                                          char *ecuid,
                                                          char *appid,
                                                          char *ctxid)
{
    char *tok = NULL;

    if (len > DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN)
        return DLT_RETURN_ERROR;

    /* copy appid and ctxid */
    tok = strtok(key, ":");

    if (tok != NULL)
        strncpy(ecuid, tok, DLT_ID_SIZE);
    else
        return DLT_RETURN_ERROR;

    tok = strtok(NULL, ":");

    if (tok != NULL)
        strncpy(appid, tok, DLT_ID_SIZE);
    else
        return DLT_RETURN_ERROR;

    strncpy(ctxid, ".*", 2);

    return DLT_RETURN_OK;
}

/**
 * dlt_logstorage_split_multi
 *
 * Prepares keys with application ID alone, will use ecuid if provided
 * (ecuid:apid::) or (:apid::)
 *
 * @param key            Prepared key stored here
 * @param len            Key length
 * @param ecuid          ECU ID
 * @param appid          Application ID
 * @param ctxid          Context ID
 * @return               None
 */
DLT_STATIC DltReturnValue dlt_logstorage_split_multi(char *key,
                                                     int len,
                                                     char *ecuid,
                                                     char *appid,
                                                     char *ctxid)
{
    char *tok = NULL;

    if (len > DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN)
        return DLT_RETURN_ERROR;

    tok = strtok(key, ":");

    if (tok == NULL)
        return DLT_RETURN_ERROR;

    len = strlen(tok);

    if (key[len + 1] == ':') {
        strncpy(ecuid, tok, DLT_ID_SIZE);

        tok = strtok(NULL, ":");

        if (tok != NULL)
            strncpy(ctxid, tok, DLT_ID_SIZE);

        strncpy(appid, ".*", 2);
    }
    else {
        strncpy(ecuid, tok, DLT_ID_SIZE);
        tok = strtok(NULL, ":");
        strncpy(appid, tok, DLT_ID_SIZE);
        tok = strtok(NULL, ":");
        strncpy(ctxid, tok, DLT_ID_SIZE);
    }

    return DLT_RETURN_OK;
}

/**
 * dlt_logstorage_split_key
 *
 * Split a given key into appid and ctxid.
 * If APID: - appid = APID and ctxid = .*
 * If :CTID - ctxid = CTID and appid = .*
 * Else appid = APID and ctxid = CTID
 *
 * @param key      Given key of filter hash map
 * @param appid    Application id
 * @param ctxid    Context id
 * @param ecuid    ECU id
 * @return         0 on success, -1 on error
 */
DLT_STATIC DltReturnValue dlt_logstorage_split_key(char *key, char *appid, char *ctxid, char *ecuid)
{
    int len = 0;
    char *sep = NULL;

    if ((key == NULL) || (appid == NULL) || (ctxid == NULL) || (ecuid == NULL))
        return DLT_RETURN_WRONG_PARAMETER;

    len = strlen(key);

    sep = strchr (key, ':');

    if (sep == NULL)
        return DLT_RETURN_WRONG_PARAMETER;

    /* key is ecuid only ecuid::*/
    if ((key[len - 1] == ':') && (key[len - 2] == ':'))
        return dlt_logstorage_split_ecuid(key, len, ecuid, appid, ctxid);
    /* key is context id only  ::apid*/
    else if ((key[0] == ':') && (key[1] == ':'))
        return dlt_logstorage_split_ctid(key, len, appid, ctxid);
    /* key is application id only :apid: */
    else if ((key[0] == ':') && (key[len - 1] == ':'))
        return dlt_logstorage_split_apid(key, len, appid, ctxid);
    /* key is :apid:ctid */
    else if ((key[0] == ':') && (key[len - 1] != ':'))
        return dlt_logstorage_split_apid_ctid(key, len, appid, ctxid);
    /* key is ecuid:apid: */
    else if ((key[0] != ':') && (key[len - 1] == ':'))
        return dlt_logstorage_split_ecuid_apid(key, len, ecuid, appid, ctxid);
    /* key is either ecuid::ctid or ecuid:apid:ctid */
    else
        return dlt_logstorage_split_multi(key, len, ecuid, appid, ctxid);
}

/**
 * Forward SET_LOG_LEVEL request to passive node
 *
 * @param daemon_local  pointer to DltDaemonLocal structure
 * @param apid          Application ID
 * @param ctid          Context ID
 * @param ecuid         ECU ID
 * @param loglevel      requested log level
 * @param verbose       verbosity flag
 */
DLT_STATIC DltReturnValue dlt_daemon_logstorage_update_passive_node_context(
    DltDaemonLocal *daemon_local,
    char *apid,
    char *ctid,
    char *ecuid,
    int loglevel,
    int verbose)
{
    DltServiceSetLogLevel req = { 0 };
    DltPassiveControlMessage ctrl = { 0 };
    DltGatewayConnection *con = NULL;

    PRINT_FUNCTION_VERBOSE(verbose);

    if ((daemon_local == NULL) || (apid == NULL) || (ctid == NULL) || (ecuid == NULL) ||
        (loglevel > DLT_LOG_VERBOSE) || (loglevel < DLT_LOG_DEFAULT)) {
        dlt_vlog(LOG_ERR, "%s: Wrong parameter\n", __func__);
        return DLT_RETURN_WRONG_PARAMETER;
    }

    con = dlt_gateway_get_connection(&daemon_local->pGateway, ecuid, verbose);

    if (con == NULL) {
        dlt_vlog(LOG_ERR,
                 "Failed to fond connection to passive node %s\n",
                 ecuid);
        return DLT_RETURN_ERROR;
    }

    ctrl.id = DLT_SERVICE_ID_SET_LOG_LEVEL;
    ctrl.type = CONTROL_MESSAGE_ON_DEMAND;

    dlt_set_id(req.apid, apid);
    dlt_set_id(req.ctid, ctid);

    req.log_level = loglevel;

    if (dlt_gateway_send_control_message(con, &ctrl, (void *)&req, verbose) != 0) {
        dlt_vlog(LOG_ERR,
                 "Failed to forward SET_LOG_LEVEL message to passive node %s\n",
                 ecuid);

        return DLT_RETURN_ERROR;
    }

    return DLT_RETURN_OK;
}

/**
 * dlt_daemon_logstorage_send_log_level
 *
 * Send new log level for the provided context, if ecuid is not daemon ecuid
 * update log level of passive node
 *
 * @param daemon            DltDaemon structure
 * @param daemon_local      DltDaemonLocal structure
 * @param context           DltDaemonContext structure
 * @param ecuid             ECU id
 * @param loglevel          log level to be set to context
 * @param verbose           If set to true verbose information is printed out
 * @return                  0 on success, -1 on error
 */
DLT_STATIC DltReturnValue dlt_daemon_logstorage_send_log_level(DltDaemon *daemon,
                                                               DltDaemonLocal *daemon_local,
                                                               DltDaemonContext *context,
                                                               char *ecuid,
                                                               int loglevel,
                                                               int verbose)
{
    int old_log_level = -1;
    int ll = DLT_LOG_DEFAULT;

    if ((daemon == NULL) || (daemon_local == NULL) || (ecuid == NULL) ||
        (context == NULL) || (loglevel > DLT_LOG_VERBOSE) || (loglevel < DLT_LOG_DEFAULT)) {
        dlt_vlog(LOG_ERR, "%s: Wrong parameter\n", __func__);
        return DLT_RETURN_WRONG_PARAMETER;
    }

    if (strncmp(ecuid, daemon->ecuid, DLT_ID_SIZE) == 0) {
        old_log_level = context->storage_log_level;

        context->storage_log_level = DLT_OFFLINE_LOGSTORAGE_MAX(loglevel,
                                                                context->storage_log_level);

        if (context->storage_log_level > old_log_level) {
            if (dlt_daemon_user_send_log_level(daemon, context, verbose) == -1) {
                dlt_log(LOG_ERR, "Unable to update log level\n");
                return DLT_RETURN_ERROR;
            }
        }
    }
    else {

        old_log_level = context->log_level;

        ll = DLT_OFFLINE_LOGSTORAGE_MAX(loglevel, context->log_level);

        if (ll > old_log_level)
            return dlt_daemon_logstorage_update_passive_node_context(daemon_local,
                                                                     context->apid,
                                                                     context->ctid,
                                                                     ecuid,
                                                                     ll,
                                                                     verbose);
    }

    return DLT_RETURN_OK;
}

/**
 * dlt_daemon_logstorage_reset_log_level
 *
 * The log levels are reset if log level provided is -1 (not sent to
 * application in this case). Reset and sent to application if current log level
 * provided is 0.
 *
 * @param daemon            DltDaemon structure
 * @param daemon_local      DltDaemonLocal structure
 * @param context           DltDaemonContext structure
 * @param ecuid             ECU ID
 * @param loglevel          log level to be set to context
 * @param verbose           If set to true verbose information is printed out
 * @return                  0 on success, -1 on error
 */
DLT_STATIC DltReturnValue dlt_daemon_logstorage_reset_log_level(DltDaemon *daemon,
                                                                DltDaemonLocal *daemon_local,
                                                                DltDaemonContext *context,
                                                                char *ecuid,
                                                                int loglevel,
                                                                int verbose)
{
    if ((daemon == NULL) || (daemon_local == NULL) || (ecuid == NULL) ||
        (context == NULL) || (loglevel > DLT_LOG_VERBOSE) || (loglevel < DLT_LOG_DEFAULT)) {
        dlt_vlog(LOG_ERR, "%s: Wrong parameter\n", __func__);
        return DLT_RETURN_WRONG_PARAMETER;
    }

    /* Set storage level to -1, to clear log levels */
    context->storage_log_level = DLT_LOG_DEFAULT;

    if (loglevel == DLT_DAEMON_LOGSTORAGE_RESET_SEND_LOGLEVEL) {
        if (strncmp(ecuid, daemon->ecuid, DLT_ID_SIZE) == 0) {
            if (dlt_daemon_user_send_log_level(daemon,
                                               context,
                                               verbose) == DLT_RETURN_ERROR) {
                dlt_log(LOG_ERR, "Unable to update log level\n");
                return DLT_RETURN_ERROR;
            }
        }
        else { /* forward set log level to passive node */
            return dlt_daemon_logstorage_update_passive_node_context(daemon_local,
                                                                     context->apid,
                                                                     context->ctid,
                                                                     ecuid,
                                                                     DLT_LOG_DEFAULT,
                                                                     verbose);
        }
    }

    return DLT_RETURN_OK;
}

/**
 * dlt_daemon_logstorage_force_reset_level
 *
 * Force resetting of log level since have no data provided by passive node.
 *
 * @param daemon            DltDaemon structure
 * @param daemon_local      DltDaemonLocal structure
 * @param apid              Application ID
 * @param ctxid             Context ID
 * @param ecuid             ECU ID
 * @param loglevel          log level to be set to context
 * @param verbose           If set to true verbose information is printed out
 * @return                  0 on success, -1 on error
 */
DLT_STATIC DltReturnValue dlt_daemon_logstorage_force_reset_level(DltDaemon *daemon,
                                                                  DltDaemonLocal *daemon_local,
                                                                  char *apid,
                                                                  char *ctxid,
                                                                  char *ecuid,
                                                                  int loglevel,
                                                                  int verbose)
{
    int ll = DLT_LOG_DEFAULT;
    int num = 0;
    int i = 0;
    DltLogStorageFilterConfig *config[DLT_CONFIG_FILE_SECTIONS_MAX] = { 0 };

    if ((daemon == NULL) || (daemon_local == NULL) || (ecuid == NULL) ||
        (apid == NULL) || (ctxid == NULL) || (loglevel > DLT_LOG_VERBOSE) || (loglevel < DLT_LOG_DEFAULT)) {
        dlt_vlog(LOG_ERR, "%s: Wrong parameter\n", __func__);
        return DLT_RETURN_WRONG_PARAMETER;
    }

    for (i = 0; i < daemon_local->flags.offlineLogstorageMaxDevices; i++) {
        num = dlt_logstorage_get_config(&(daemon->storage_handle[i]), config, apid, ctxid, ecuid);

        if (num > 0)
            break; /* found config */
    }

    if ((num == 0) || (config[0] == NULL)) {
        dlt_vlog(LOG_ERR,
                 "%s: No information about APID: %s, CTID: %s, ECU: %s in Logstorage configuration\n",
                 __func__, apid, ctxid, ecuid);
        return DLT_RETURN_ERROR;
    }

    if (loglevel == DLT_DAEMON_LOGSTORAGE_RESET_SEND_LOGLEVEL)
        ll = config[0]->reset_log_level;
    else
        ll = config[0]->log_level;

    return dlt_daemon_logstorage_update_passive_node_context(daemon_local, apid,
                                                             ctxid, ecuid, ll, verbose);

}

/**
 * dlt_logstorage_update_all_contexts
 *
 * Update log level of all contexts of the application by updating the daemon
 * internal table. The compare flags (cmp_flag) indicates if Id has to be
 * compared with application id or Context id of the daemon internal table.
 * The log levels are reset if current log level provided is -1 (not sent to
 * application in this case). Reset and sent to application if current log level
 * provided is 0.
 *
 * @param daemon            DltDaemon structure
 * @param daemon_local      DltDaemonLocal structure
 * @param id                application id or context id
 * @param curr_log_level    log level to be set to context
 * @param cmp_flag          compare flag (1 id is apid, 2 id is ctxid)
 * @param ecuid             ecu id where application runs
 * @param verbose           If set to true verbose information is printed out
 * @return                  0 on success, -1 on error
 */
DltReturnValue dlt_logstorage_update_all_contexts(DltDaemon *daemon,
                                                  DltDaemonLocal *daemon_local,
                                                  char *id,
                                                  int curr_log_level,
                                                  int cmp_flag,
                                                  char *ecuid,
                                                  int verbose)
{
    DltDaemonRegisteredUsers *user_list = NULL;
    int i = 0;
    char tmp_id[DLT_ID_SIZE + 1] = { '\0' };

    if ((daemon == NULL) || (daemon_local == NULL) || (id == NULL) ||
        (ecuid == NULL) || (cmp_flag < DLT_DAEMON_LOGSTORAGE_CMP_APID) ||
        (cmp_flag > DLT_DAEMON_LOGSTORAGE_CMP_CTID)) {
        dlt_vlog(LOG_ERR, "Wrong parameter in function %s\n", __func__);
        return DLT_RETURN_WRONG_PARAMETER;
    }

    user_list = dlt_daemon_find_users_list(daemon, ecuid, verbose);

    if (user_list == NULL)
        return DLT_RETURN_ERROR;

    for (i = 0; i < user_list->num_contexts; i++) {
        if (cmp_flag == 1)
            dlt_set_id(tmp_id, user_list->contexts[i].apid);
        else
            dlt_set_id(tmp_id, user_list->contexts[i].ctid);

        if (strncmp(id, tmp_id, DLT_ID_SIZE) == 0) {
            if (curr_log_level > 0)
                dlt_daemon_logstorage_send_log_level(daemon,
                                                     daemon_local,
                                                     &user_list->contexts[i],
                                                     ecuid,
                                                     curr_log_level,
                                                     verbose);
            else /* The request is to reset log levels */
                dlt_daemon_logstorage_reset_log_level(daemon,
                                                      daemon_local,
                                                      &user_list->contexts[i],
                                                      ecuid,
                                                      curr_log_level,
                                                      verbose);
        }
    }

    return DLT_RETURN_OK;
}

/**
 * dlt_logstorage_update_context
 *
 * Update log level of a context by updating the daemon internal table
 * The log levels are reset if current log level provided is -1 (not sent to
 * application in this case)
 * Reset and sent to application if current log level provided is 0
 *
 * @param daemon            DltDaemon structure
 * @param daemon_local      DltDaemonLocal structure
 * @param apid              application id
 * @param ctxid             context id
 * @param ecuid             ecu id
 * @param curr_log_level    log level to be set to context
 * @param verbose           If set to true verbose information is printed out
 * @return                  0 on success, -1 on error
 */
DltReturnValue dlt_logstorage_update_context(DltDaemon *daemon,
                                             DltDaemonLocal *daemon_local,
                                             char *apid,
                                             char *ctxid,
                                             char *ecuid,
                                             int curr_log_level,
                                             int verbose)
{
    DltDaemonContext *context = NULL;

    if ((daemon == NULL) || (daemon_local == NULL) || (apid == NULL)
        || (ctxid == NULL) || (ecuid == NULL)) {
        dlt_vlog(LOG_ERR, "Wrong parameter in function %s\n", __func__);
        return DLT_RETURN_WRONG_PARAMETER;
    }

    context = dlt_daemon_context_find(daemon, apid, ctxid, ecuid, verbose);

    if (context != NULL) {
        if (curr_log_level > 0)
            return dlt_daemon_logstorage_send_log_level(daemon,
                                                        daemon_local,
                                                        context,
                                                        ecuid,
                                                        curr_log_level,
                                                        verbose);
        else /* The request is to reset log levels */
            return dlt_daemon_logstorage_reset_log_level(daemon,
                                                         daemon_local,
                                                         context,
                                                         ecuid,
                                                         curr_log_level,
                                                         verbose);
    }
    else {
        if (strncmp(ecuid, daemon->ecuid, DLT_ID_SIZE) != 0) {
            /* we intentionally have no data provided by passive node. */
            /* We blindly send the log level or reset log level */
            return dlt_daemon_logstorage_force_reset_level(daemon,
                                                           daemon_local,
                                                           apid,
                                                           ctxid,
                                                           ecuid,
                                                           curr_log_level,
                                                           verbose);
        }
        else {
            dlt_vlog(LOG_WARNING,
                     "%s: No information about APID: %s, CTID: %s, ECU: %s\n",
                     __func__,
                     apid,
                     ctxid,
                     ecuid);
            return DLT_RETURN_ERROR;

        }
    }

    return DLT_RETURN_OK;
}

/**
 * dlt_logstorage_update_context_loglevel
 *
 * Update all contexts or particular context depending provided key
 *
 * @param daemon            Pointer to DLT Daemon structure
 * @param daemon_local      Pointer to DLT Daemon Local structure
 * @param key               Filter key stored in Hash Map
 * @param curr_log_level    log level to be set to context
 * @param verbose           If set to true verbose information is printed out
 * @return                  0 on success, -1 on error
 */
DltReturnValue dlt_logstorage_update_context_loglevel(DltDaemon *daemon,
                                                      DltDaemonLocal *daemon_local,
                                                      char *key,
                                                      int curr_log_level,
                                                      int verbose)
{
    int cmp_flag = 0;
    char appid[DLT_ID_SIZE + 1] = { '\0' };
    char ctxid[DLT_ID_SIZE + 1] = { '\0' };
    char ecuid[DLT_ID_SIZE + 1] = { '\0' };

    PRINT_FUNCTION_VERBOSE(verbose);

    if ((daemon == NULL) || (daemon_local == NULL) || (key == NULL))
        return DLT_RETURN_WRONG_PARAMETER;

    if (dlt_logstorage_split_key(key, appid, ctxid, ecuid) != 0) {
        dlt_log(LOG_ERR,
                "Error while updating application log levels (split key)\n");
        return DLT_RETURN_ERROR;
    }

    if (ecuid[0] == '\0') /* ECU id was not specified in filter configuration */
        dlt_set_id(ecuid, daemon->ecuid);

    /* wildcard for context id, find all contexts of given application id */
    if (strcmp(ctxid, ".*") == 0) {
        cmp_flag = DLT_DAEMON_LOGSTORAGE_CMP_APID;

        if (dlt_logstorage_update_all_contexts(daemon,
                                               daemon_local,
                                               appid,
                                               curr_log_level,
                                               cmp_flag,
                                               ecuid,
                                               verbose) != 0)
            return DLT_RETURN_ERROR;
    }
    /* wildcard for application id, find all contexts with context id */
    else if (strcmp(appid, ".*") == 0)
    {
        cmp_flag = DLT_DAEMON_LOGSTORAGE_CMP_CTID;

        if (dlt_logstorage_update_all_contexts(daemon,
                                               daemon_local,
                                               ctxid,
                                               curr_log_level,
                                               cmp_flag,
                                               ecuid,
                                               verbose) != 0)
            return DLT_RETURN_ERROR;
    }
    /* In case of given application id, context id pair, call available context
     * find function */
    else if (dlt_logstorage_update_context(daemon,
                                           daemon_local,
                                           appid,
                                           ctxid,
                                           ecuid,
                                           curr_log_level,
                                           verbose) != 0)
    {
        return DLT_RETURN_ERROR;
    }

    return DLT_RETURN_OK;
}

/**
 * dlt_daemon_logstorage_reset_application_loglevel
 *
 * Reset storage log level of all running applications
 * 2 steps for resetting
 * 1. Setup storage_loglevel of all contexts configured for the requested device
 *    to -1
 * 2. Re-run update log level for all other configured devices
 *
 * @param daemon        Pointer to DLT Daemon structure
 * @param daemon_local  Pointer to DLT Daemon local structure
 * @param dev_num       Number of attached DLT Logstorage device
 * @param max_device    Maximum storage devices setup by the daemon
 * @param verbose       If set to true verbose information is printed out
 */
void dlt_daemon_logstorage_reset_application_loglevel(DltDaemon *daemon,
                                                      DltDaemonLocal *daemon_local,
                                                      int dev_num,
                                                      int max_device,
                                                      int verbose)
{
    DltLogStorage *handle = NULL;
    DltLogStorageFilterList **tmp = NULL;
    int i = 0;
    char key[DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN + 1] = { '\0' };
    int num_device_configured = 0;
    unsigned int status;
    int log_level = 0;

    PRINT_FUNCTION_VERBOSE(verbose);

    if ((daemon == NULL) || (daemon_local == NULL) ||
        (daemon->storage_handle == NULL) || (dev_num < 0)) {
        dlt_vlog(LOG_ERR,
                 "Invalid function parameters used for %s\n",
                 __func__);
        return;
    }

    handle = &(daemon->storage_handle[dev_num]);

    if ((handle->connection_type != DLT_OFFLINE_LOGSTORAGE_DEVICE_CONNECTED) ||
        (handle->config_status != DLT_OFFLINE_LOGSTORAGE_CONFIG_DONE))
        return;

    /* First, check number of devices configured */
    for (i = 0; i < max_device; i++) {
        status = daemon->storage_handle[i].config_status;

        if (status == DLT_OFFLINE_LOGSTORAGE_CONFIG_DONE)
            num_device_configured++;
    }

    /* for all filters (keys) check if application context are already running
     * and log level need to be reset*/
    tmp = &(handle->config_list);
    while (*(tmp) != NULL)
    {
        for (i = 0; i < (*tmp)->num_keys; i++)
        {
            memset(key, 0, sizeof(key));

            strncpy(key, ((*tmp)->key_list
                          + (i * DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN)),
                    DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN);

            if (num_device_configured == 1)
            {
                /* Reset context log level and send to application */
                log_level = DLT_DAEMON_LOGSTORAGE_RESET_SEND_LOGLEVEL;
            }
            else
            {
                /**
                 * Reset context log level do not send to application as other
                 * devices can have same configuration
                 * */
                log_level = DLT_DAEMON_LOGSTORAGE_RESET_LOGLEVEL;
            }

            dlt_logstorage_update_context_loglevel(
                    daemon,
                    daemon_local,
                    key,
                    log_level,
                    verbose);
        }
        tmp = &(*tmp)->next;
    }

    /* Re-run update log level for all other configured devices */
    for (i = 0; i < max_device; i++) {
        status = daemon->storage_handle[i].config_status;

        if (i == dev_num)
            continue;

        if (status == DLT_OFFLINE_LOGSTORAGE_CONFIG_DONE)
            dlt_daemon_logstorage_update_application_loglevel(daemon,
                                                              daemon_local,
                                                              i,
                                                              verbose);
    }

    return;
}

/**
 * dlt_daemon_logstorage_update_application_loglevel
 *
 * Update log level of all running applications with new filter configuration
 * available due to newly attached DltLogstorage device. The log level is only
 * updated when the current application log level is less than the log level
 * obtained from the storage configuration file
 *
 * @param daemon        Pointer to DLT Daemon structure
 * @param daemon_local  Pointer to DLT Daemon local structure
 * @param dev_num       Number of attached DLT Logstorage device
 * @param verbose       If set to true verbose information is printed out
 */
void dlt_daemon_logstorage_update_application_loglevel(DltDaemon *daemon,
                                                       DltDaemonLocal *daemon_local,
                                                       int dev_num,
                                                       int verbose)
{
    DltLogStorage *handle = NULL;
    DltLogStorageFilterList **tmp = NULL;
    char key[DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN + 1] = { '\0' };
    int i = 0;
    int log_level = 0;

    PRINT_FUNCTION_VERBOSE(verbose);

    if ((daemon == NULL) || (daemon_local == NULL) || (dev_num < 0))
    {
        dlt_vlog(LOG_ERR,
                 "Invalid function parameters used for %s\n",
                 __func__);
        return;
    }

    handle = &(daemon->storage_handle[dev_num]);

    if ((handle->connection_type != DLT_OFFLINE_LOGSTORAGE_DEVICE_CONNECTED) ||
        (handle->config_status != DLT_OFFLINE_LOGSTORAGE_CONFIG_DONE))
        return;

    /* for all filters (keys) check if application or context already running
     * and log level need to be updated*/
    tmp = &(handle->config_list);
    while (*(tmp) != NULL)
    {
        for (i = 0; i < (*tmp)->num_keys; i++)
        {
            memset(key, 0, sizeof(key));

            strncpy(key, ((*tmp)->key_list
                    + (i * DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN)),
                    DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN);

            /* Obtain storage configuration data */
            log_level = dlt_logstorage_get_loglevel_by_key(handle, key);
            if (log_level < 0)
            {
                dlt_log(LOG_ERR, "Failed to get log level by key \n");
                return;
            }

            /* Update context log level with storage configuration log level */
            dlt_logstorage_update_context_loglevel(daemon,
                                                daemon_local,
                                                key,
                                                log_level,
                                                verbose);
        }
        tmp = &(*tmp)->next;
    }

    return;
}

/**
 * dlt_daemon_logstorage_get_loglevel
 *
 * Obtain log level as a union of all configured storage devices and filters for
 * the provided application id and context id
 *
 * @param daemon        Pointer to DLT Daemon structure
 * @param max_device    Maximum storage devices setup by the daemon
 * @param apid          Application ID
 * @param ctid          Context ID
 * @return              Log level on success, -1 on error
 */
int dlt_daemon_logstorage_get_loglevel(DltDaemon *daemon,
                                       int max_device,
                                       char *apid,
                                       char *ctid)
{
    DltLogStorageFilterConfig *config[DLT_CONFIG_FILE_SECTIONS_MAX] = { 0 };
    int i = 0;
    int j = 0;
    int8_t storage_loglevel = -1;
    int8_t configured_loglevel = -1;
    int num_config = 0;

    if ((daemon == NULL) || (max_device == 0) || (apid == NULL) || (ctid == NULL))
        return DLT_RETURN_WRONG_PARAMETER;

    for (i = 0; i < max_device; i++)
        if (daemon->storage_handle[i].config_status ==
            DLT_OFFLINE_LOGSTORAGE_CONFIG_DONE) {
            num_config = dlt_logstorage_get_config(&(daemon->storage_handle[i]),
                                                   config,
                                                   apid,
                                                   ctid,
                                                   daemon->ecuid);

            if (num_config == 0) {
                dlt_log(LOG_DEBUG, "No valid filter configuration found\n");
                continue;
            }

            for (j = 0; j < num_config; j++)
            {
                if (config[j] == NULL)
                    continue;

                /* If logstorage configuration do not contain file name,
                 * then it is non verbose control filter, so return level as in this filter */
                if (config[j]->file_name == NULL) {
                    storage_loglevel = config[j]->log_level;
                    break;
                }

                configured_loglevel = config[j]->log_level;
                storage_loglevel = DLT_OFFLINE_LOGSTORAGE_MAX(
                    configured_loglevel,
                    storage_loglevel);
            }
        }

    return storage_loglevel;
}

/**
 * dlt_daemon_logstorage_write
 *
 * Write log message to all attached storage device. If the called
 * dlt_logstorage_write function is not able to write to the device, DltDaemon
 * will disconnect this device.
 *
 * @param daemon        Pointer to Dlt Daemon structure
 * @param user_config   DltDaemon configuration
 * @param data1         message header buffer
 * @param size1         message header buffer size
 * @param data2         message extended header buffer
 * @param size2         message extended header size
 * @param data3         message data buffer
 * @param size3         message data size
 */
void dlt_daemon_logstorage_write(DltDaemon *daemon,
                                 DltDaemonFlags *user_config,
                                 unsigned char *data1,
                                 int size1,
                                 unsigned char *data2,
                                 int size2,
                                 unsigned char *data3,
                                 int size3)
{
    int i = 0;
    DltLogStorageUserConfig file_config;

    if ((daemon == NULL) || (user_config == NULL) ||
        (user_config->offlineLogstorageMaxDevices <= 0) || (data1 == NULL) ||
        (data2 == NULL) || (data3 == NULL)) {
        dlt_vlog(LOG_DEBUG,
                 "%s: message type is not LOG. Skip storing.\n",
                 __func__);
        return;
        /* Log Level changed callback */
    }

    /* Copy user configuration */
    file_config.logfile_timestamp = user_config->offlineLogstorageTimestamp;
    file_config.logfile_delimiter = user_config->offlineLogstorageDelimiter;
    file_config.logfile_maxcounter = user_config->offlineLogstorageMaxCounter;
    file_config.logfile_counteridxlen =
        user_config->offlineLogstorageMaxCounterIdx;

    for (i = 0; i < user_config->offlineLogstorageMaxDevices; i++)
        if (daemon->storage_handle[i].config_status ==
            DLT_OFFLINE_LOGSTORAGE_CONFIG_DONE) {
            if (dlt_logstorage_write(&(daemon->storage_handle[i]),
                                     &file_config,
                                     data1,
                                     size1,
                                     data2,
                                     size2,
                                     data3,
                                     size3) != 0) {
                dlt_log(LOG_ERR,
                        "dlt_daemon_logstorage_write: failed. "
                        "Disable storage device\n");
                /* DLT_OFFLINE_LOGSTORAGE_MAX_WRITE_ERRORS happened,
                 * therefore remove logstorage device */
                dlt_logstorage_device_disconnected(
                    &(daemon->storage_handle[i]),
                    DLT_LOGSTORAGE_SYNC_ON_DEVICE_DISCONNECT);
            }
        }
}

/**
 * dlt_daemon_logstorage_setup_internal_storage
 *
 * Setup user defined path as offline log storage device
 *
 * @param daemon        Pointer to Dlt Daemon structure
 * @param daemon_local  Pointer to Dlt Daemon local structure
 * @param path          User configured internal storage path
 * @param verbose       If set to true verbose information is printed out
 * @return 0 on sucess, -1 otherwise
 */
int dlt_daemon_logstorage_setup_internal_storage(DltDaemon *daemon,
                                                 DltDaemonLocal *daemon_local,
                                                 char *path,
                                                 int verbose)
{
    int ret = 0;

    PRINT_FUNCTION_VERBOSE(verbose);

    if ((path == NULL) || (daemon == NULL))
        return DLT_RETURN_WRONG_PARAMETER;

    /* connect internal storage device */
    /* Device index always used as 0 as it is setup on DLT daemon startup */
    ret = dlt_logstorage_device_connected(&(daemon->storage_handle[0]), path);

    if (ret != 0) {
        dlt_vlog(LOG_ERR, "%s: Device connect failed\n", __func__);
        return DLT_RETURN_ERROR;
    }

    /* check if log level of running application need an update */
    dlt_daemon_logstorage_update_application_loglevel(daemon,
                                                      daemon_local,
                                                      0,
                                                      verbose);

    return ret;
}

void dlt_daemon_logstorage_set_logstorage_cache_size(unsigned int size)
{
    /* store given [KB] size in [Bytes] */
    g_logstorage_cache_max = size * 1024;
}

int dlt_daemon_logstorage_cleanup(DltDaemon *daemon,
                                  DltDaemonLocal *daemon_local,
                                  int verbose)
{
    int i = 0;

    PRINT_FUNCTION_VERBOSE(verbose);

    if ((daemon == NULL) || (daemon_local == NULL) || (daemon->storage_handle == NULL))
        return DLT_RETURN_WRONG_PARAMETER;

    for (i = 0; i < daemon_local->flags.offlineLogstorageMaxDevices; i++)
        /* call disconnect on all currently connected devices */
        if (daemon->storage_handle[i].connection_type ==
            DLT_OFFLINE_LOGSTORAGE_DEVICE_CONNECTED)
        {
            (&daemon->storage_handle[i])->uconfig.logfile_counteridxlen =
                                        daemon_local->flags.offlineLogstorageMaxCounterIdx;
            (&daemon->storage_handle[i])->uconfig.logfile_delimiter =
                                        daemon_local->flags.offlineLogstorageDelimiter;
            (&daemon->storage_handle[i])->uconfig.logfile_maxcounter =
                                        daemon_local->flags.offlineLogstorageMaxCounter;
            (&daemon->storage_handle[i])->uconfig.logfile_timestamp =
                                        daemon_local->flags.offlineLogstorageTimestamp;

            dlt_logstorage_device_disconnected(
                &daemon->storage_handle[i],
                DLT_LOGSTORAGE_SYNC_ON_DAEMON_EXIT);
        }

    return 0;
}

int dlt_daemon_logstorage_sync_cache(DltDaemon *daemon,
                                     DltDaemonLocal *daemon_local,
                                     char *mnt_point,
                                     int verbose)
{
    int i = 0;
    DltLogStorage *handle = NULL;

    PRINT_FUNCTION_VERBOSE(verbose);

    if ((daemon == NULL) || (daemon_local == NULL) || (mnt_point == NULL))
        return DLT_RETURN_WRONG_PARAMETER;

    if (strlen(mnt_point) > 0) { /* mount point is given */
        handle = dlt_daemon_logstorage_get_device(daemon,
                                                  daemon_local,
                                                  mnt_point,
                                                  verbose);

        if (handle == NULL) {
            return DLT_RETURN_ERROR;
        }
        else {
            handle->uconfig.logfile_counteridxlen =
                daemon_local->flags.offlineLogstorageMaxCounterIdx;
            handle->uconfig.logfile_delimiter =
                daemon_local->flags.offlineLogstorageDelimiter;
            handle->uconfig.logfile_maxcounter =
                daemon_local->flags.offlineLogstorageMaxCounter;
            handle->uconfig.logfile_timestamp =
                daemon_local->flags.offlineLogstorageTimestamp;

            if (dlt_logstorage_sync_caches(handle) != 0)
                return DLT_RETURN_ERROR;
        }
    }
    else { /* sync caches for all connected logstorage devices */

        for (i = 0; i < daemon_local->flags.offlineLogstorageMaxDevices; i++)
            if (daemon->storage_handle[i].connection_type ==
                DLT_OFFLINE_LOGSTORAGE_DEVICE_CONNECTED) {
                daemon->storage_handle[i].uconfig.logfile_counteridxlen =
                    daemon_local->flags.offlineLogstorageMaxCounterIdx;
                daemon->storage_handle[i].uconfig.logfile_delimiter =
                    daemon_local->flags.offlineLogstorageDelimiter;
                daemon->storage_handle[i].uconfig.logfile_maxcounter =
                    daemon_local->flags.offlineLogstorageMaxCounter;
                daemon->storage_handle[i].uconfig.logfile_timestamp =
                    daemon_local->flags.offlineLogstorageTimestamp;

                if (dlt_logstorage_sync_caches(&daemon->storage_handle[i]) != 0)
                    return DLT_RETURN_ERROR;
            }
    }

    return 0;
}

DltLogStorage *dlt_daemon_logstorage_get_device(DltDaemon *daemon,
                                                DltDaemonLocal *daemon_local,
                                                char *mnt_point,
                                                int verbose)
{
    int i = 0;
    int len = 0;
    int len1 = 0;
    int len2 = 0;

    PRINT_FUNCTION_VERBOSE(verbose);

    if ((daemon == NULL) || (daemon_local == NULL) || (mnt_point == NULL))
        return NULL;

    len1 = strlen(mnt_point);

    for (i = 0; i < daemon_local->flags.offlineLogstorageMaxDevices; i++) {
        len2 = strlen(daemon->storage_handle[i].device_mount_point);

        /* Check if the requested device path is already used as log storage
         * device. Check for strlen first, to avoid comparison errors when
         * final '/' is given or not */
        len = len1 > len2 ? len2 : len1;

        if (strncmp(daemon->storage_handle[i].device_mount_point, mnt_point, len) == 0)
            return &daemon->storage_handle[i];
    }

    return NULL;
}