summaryrefslogtreecommitdiff
path: root/src/offlinelogstorage/dlt_offline_logstorage.c
blob: 872c47efd0d227ab15faa8949fb647c3ed4c1498 (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
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
/**
 * Copyright (C) 2013 - 2015  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_offline_logstorage.c
 * For further information see http://www.genivi.org/.
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <ctype.h>
#include <syslog.h>
#include <sys/stat.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <time.h>

#include "dlt_offline_logstorage.h"
#include "dlt_offline_logstorage_internal.h"
#include "dlt_offline_logstorage_behavior.h"
#include "dlt_config_file_parser.h"

#define DLT_OFFLINE_LOGSTORAGE_FILTER_ERROR 1
#define DLT_OFFLINE_LOGSTORAGE_STORE_FILTER_ERROR 2
#define DLT_OFFLINE_LOGSTORAGE_FILTER_CONTINUE 3

#define GENERAL_BASE_NAME "General"

DLT_STATIC void dlt_logstorage_filter_config_free(DltLogStorageFilterConfig *data)
{
    DltLogStorageFileList *n = NULL;
    DltLogStorageFileList *n1 = NULL;

    free(data->apids);
    data->apids = NULL;
    free(data->ctids);
    data->ctids = NULL;
    free(data->file_name);
    data->file_name = NULL;

    if (data->ecuid != NULL) {
        free(data->ecuid);
        data->ecuid = NULL;
    }

    if (data->log != NULL)
        fclose(data->log);

    if (data->cache != NULL) {
        free(data->cache);
        data->cache = NULL;
    }

    n = data->records;

    while (n) {
        n1 = n;
        n = n->next;
        free(n1->name);
        n1->name = NULL;
        free(n1);
        n1 = NULL;
    }
}

/**
 * dlt_logstorage_list_destroy
 *
 * Destroy Filter configurations list.
 *
 * @param list List of the filter configurations will be destroyed.
 * @param uconfig User configurations for log file
 * @param dev_path Path to the device
 * @param reason Reason for the destroying of Filter configurations list
 * @return 0 on success, -1 on error
 */
DLT_STATIC int dlt_logstorage_list_destroy(DltLogStorageFilterList **list,
                                           DltLogStorageUserConfig *uconfig,
                                           char *dev_path,
                                           int reason)
{
    DltLogStorageFilterList *tmp = NULL;

    while (*(list) != NULL) {
        tmp = *list;
        *list = (*list)->next;
        if (tmp->key_list != NULL)
        {
            free(tmp->key_list);
            tmp->key_list = NULL;
        }

        if (tmp->data != NULL) {
            /* sync data if necessary */
            /* ignore return value */
            tmp->data->dlt_logstorage_sync(tmp->data,
                                           uconfig,
                                           dev_path,
                                           reason);

            dlt_logstorage_filter_config_free(tmp->data);

            free(tmp->data);
            tmp->data = NULL;
        }

        free(tmp);
        tmp = NULL;
    }

    return 0;
}

DLT_STATIC int dlt_logstorage_list_add_config(DltLogStorageFilterConfig *data,
                                              DltLogStorageFilterConfig **listdata)
{
    if (*(listdata) == NULL)
        return -1;

    (*listdata)->apids = NULL;
    (*listdata)->ctids = NULL;
    (*listdata)->file_name = NULL;
    (*listdata)->ecuid = NULL;
    (*listdata)->records = NULL;
    (*listdata)->log = NULL;
    (*listdata)->cache = NULL;

    /* copy the data to list */
    memcpy(*listdata, data, sizeof(DltLogStorageFilterConfig));

    if (data->apids != NULL)
        (*listdata)->apids = strdup(data->apids);

    if (data->ctids != NULL)
        (*listdata)->ctids = strdup(data->ctids);

    if (data->file_name != NULL)
        (*listdata)->file_name = strdup(data->file_name);

    if (data->ecuid != NULL)
        (*listdata)->ecuid = strdup(data->ecuid);

    return 0;
}

/**
 * dlt_logstorage_list_add
 *
 * Add Filter configurations to the list.
 *
 * @param keys Keys will be added to the list.
 * @param num_keys Number of keys
 * @param data Filter configurations data will be added to the list.
 * @param list List of the filter configurations
 * @return 0 on success, -1 on error
 */
DLT_STATIC int dlt_logstorage_list_add(char *keys,
                                       int num_keys,
                                       DltLogStorageFilterConfig *data,
                                       DltLogStorageFilterList **list)
{
    DltLogStorageFilterList *tmp = NULL;

    while (*(list) != NULL) {
        list = &(*list)->next;
    }

    tmp = calloc(1, sizeof(DltLogStorageFilterList));

    if (tmp == NULL)
        return -1;

    tmp->key_list = (char *)calloc(
                (num_keys * DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN), sizeof(char));
    if (tmp->key_list == NULL)
    {
        free(tmp);
        tmp = NULL;
        return -1;
    }

    memcpy(tmp->key_list, keys, num_keys * DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN);
    tmp->num_keys = num_keys;
    tmp->next = NULL;
    tmp->data = calloc(1, sizeof(DltLogStorageFilterConfig));

    if (tmp->data == NULL) {
        free(tmp->key_list);
        tmp->key_list = NULL;
        free(tmp);
        tmp = NULL;
        return -1;
    }

    if (dlt_logstorage_list_add_config(data, &(tmp->data)) != 0) {
        free(tmp->key_list);
        tmp->key_list = NULL;
        free(tmp->data);
        tmp->data = NULL;
        free(tmp);
        tmp = NULL;
        return -1;
    }

    *list = tmp;

    return 0;
}

/**
 * dlt_logstorage_list_find
 *
 * Find all Filter configurations corresponding with key provided.
 *
 * @param key Key to find the filter configurations
 * @param list List of the filter configurations
 * @param config Filter configurations corresponding with the key.
 * @return Number of the filter configuration found.
 */
DLT_STATIC int dlt_logstorage_list_find(char *key,
                                        DltLogStorageFilterList **list,
                                        DltLogStorageFilterConfig **config)
{
    int i = 0;
    int num = 0;

    while (*(list) != NULL) {
        for (i = 0; i < (*list)->num_keys; i++)
        {
            if (strncmp(((*list)->key_list
                        + (i * DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN)),
                        key, DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN) == 0)
            {
                config[num] = (*list)->data;
                num++;
                break;
            }
        }
        list = &(*list)->next;
    }

    return num;
}

/* Configuration file parsing helper functions */

DLT_STATIC int dlt_logstorage_count_ids(const char *str)
{

    if (str == NULL)
        return -1;

    /* delimiter is: "," */
    const char *p = str;
    int i = 0;
    int num = 1;

    while (p[i] != 0) {
        if (p[i] == ',')
            num++;

        i++;
    }

    return num;
}

/**
 * dlt_logstorage_free
 *
 * Free all allocated memory used in log storage handle
 *
 * @param handle         DLT Logstorage handle
 * @param reason         Reason for freeing the device
 *
 */
void dlt_logstorage_free(DltLogStorage *handle, int reason)
{
    if (handle == NULL) {
        dlt_vlog(LOG_ERR, "%s failed: handle is NULL\n", __func__);
        return;
    }

    dlt_logstorage_list_destroy(&(handle->config_list), &handle->uconfig,
                                handle->device_mount_point, reason);
}


/**
 * dlt_logstorage_read_list_of_names
 *
 * Evaluate app and ctx names given in config file and create a list of names
 * acceptable by DLT Daemon. When using SET_APPLICATION_NAME and SET_CONTEXT_NAME
 * there is no constraint that these names have max 4 characters. Internally,
 * these names are cutted down to max 4 chars. To have create valid keys, the
 * internal representation of these names has to be considered.
 * Therefore, a given configuration of "AppLogName = App1,Application2,A3" will
 * be stored as "App1,Appl,A3".
 *
 * @param names        to store the list of names
 * @param value        string given in config file
 * @return             0 on success, -1 on error
 */
DLT_STATIC int dlt_logstorage_read_list_of_names(char **names, char *value)
{
    int i = 0;
    int y = 0;
    int len = 0;
    char *tok;
    int num = 1;

    if ((names == NULL) || (value == NULL))
        return -1;

    /* free, alloce'd memory to store new apid/ctid */
    if (*names != NULL) {
        free(*names);
        *names = NULL;
    }

    len = strlen(value);

    if (len == 0)
        return -1;

    /* count number of delimiters to get actual number off names */
    num = dlt_logstorage_count_ids(value);

    /* need to alloc space for 5 chars, 4 for the name and "," and "\0" */
    *names = (char *)calloc(num * 5, sizeof(char));

    if (*names == NULL)
        return -1;

    tok = strtok(value, ",");

    i = 1;

    while (tok != NULL) {
        len = strlen(tok);
        len = DLT_OFFLINE_LOGSTORAGE_MIN(len, 4);

        strncpy((*names + y), tok, len);

        if ((num > 1) && (i < num))
            strncpy((*names + y + len), ",", 2);

        y += len + 1;

        i++;
        tok = strtok(NULL, ",");
    }

    return 0;
}

/**
 * dlt_logstorage_read_number
 *
 * Evaluate file size and number of files given in config file and set file size
 * The file number is checked by converting a string to an unsigned integer
 * width 0 > result < UINT_MAX (excludes 0!)
 * Non-digit characters including spaces and out of boundary will lead to an
 * error -1.
 *
 * @param number       Number to be read
 * @param value        string given in config file
 * @return             0 on success, -1 on error
 */
DLT_STATIC int dlt_logstorage_read_number(unsigned int *number, char *value)
{
    int i = 0;
    int len = 0;
    unsigned long size = 0;

    if (value == NULL)
        return -1;

    *number = 0;
    len = strlen(value);

    /* check if string consists of digits only */
    for (i = 0; i < len; i++)
        if (!isdigit(value[i])) {
            dlt_log(LOG_ERR, "Invalid, is not a number \n");
            return -1;
        }

    size = strtoul(value, NULL, 10);

    if ((size == 0) || (size > UINT_MAX)) {
        dlt_log(LOG_ERR, "Invalid, is not a number \n");
        return -1;
    }

    *number = (unsigned int)size;

    return 0;
}

/**
 * dlt_logstorage_get_keys_list
 *
 * Obtain key list and number of keys for id list passed
 * after splitting it between seperator (,)
 *
 * @param ids            ID's
 * @param sep            Seperator
 * @param list           Prepared key list is stored here
 * @param numids         Number of keys in the list is stored here
 * @return: 0 on success, error on failure*
 */
DLT_STATIC int dlt_logstorage_get_keys_list(char *ids, char *sep, char **list,
                                            int *numids)
{
    char *token = NULL;
    char *tmp_token = NULL;
    char *ids_local = NULL;

    *numids = 0;

    /* Duplicate the ids passed for using in strtok_r() */
    ids_local = strdup(ids);

    if (ids_local == NULL)
        return -1;

    token = strtok_r(ids_local, sep, &tmp_token);

    if (token == NULL) {
        free(ids_local);
        return -1;
    }

    *list = (char *)calloc(DLT_OFFLINE_LOGSTORAGE_MAXIDS * (DLT_ID_SIZE + 1),
                           sizeof(char));

    if (*(list) == NULL) {
        free(ids_local);
        return -1;
    }

    while (token != NULL) {
        /* If it reached the max then other ids are ignored */
        if (*numids >= DLT_OFFLINE_LOGSTORAGE_MAXIDS) {
            free(ids_local);
            return 0;
        }

        strncpy(((*list) + ((*numids) * (DLT_ID_SIZE + 1))), token,
                DLT_ID_SIZE);
        *numids = *numids + 1;
        token = strtok_r(NULL, sep, &tmp_token);
    }

    free(ids_local);

    return 0;
}

/**
 * dlt_logstorage_create_keys_only_ctid
 *
 * Prepares keys with context ID alone, will use ecuid if provided
 * (ecuid\:\:ctid) or (\:\:ctid)
 *
 * @param ecuid          ECU ID
 * @param ctid           Context ID
 * @param key            Prepared key stored here
 * @return               None
 */
DLT_STATIC void dlt_logstorage_create_keys_only_ctid(char *ecuid, char *ctid,
                                                     char *key)
{
    char curr_str[DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN + 1] = { 0 };
    int curr_len = 0;

    if (ecuid != NULL) {
        strncpy(curr_str, ecuid, strlen(ecuid));
        strncat(curr_str, "::", 2);
    }
    else {
        strncpy(curr_str, "::", 2);
    }

    curr_len = strlen(ctid);
    strncat(curr_str, ctid, curr_len);
    curr_len = strlen(curr_str);

    strncpy(key, curr_str, curr_len);
}

/**
 * dlt_logstorage_create_keys_only_apid
 *
 * Prepares keys with application ID alone, will use ecuid if provided
 * (ecuid:apid::) or (:apid::)
 *
 * @param ecuid          ECU ID
 * @param apid           Application ID
 * @param key            Prepared key stored here
 * @return               None
 */
DLT_STATIC void dlt_logstorage_create_keys_only_apid(char *ecuid, char *apid,
                                                     char *key)
{
    char curr_str[DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN + 1] = { 0 };
    int curr_len = 0;

    if (ecuid != NULL) {
        strncpy(curr_str, ecuid, strlen(ecuid));
        strncat(curr_str, ":", 1);
    }
    else {
        strncpy(curr_str, ":", 1);
    }

    curr_len = strlen(apid);
    strncat(curr_str, apid, curr_len);
    strncat(curr_str, ":", 1);
    curr_len = strlen(curr_str);

    strncpy(key, curr_str, curr_len);
}

/**
 * dlt_logstorage_create_keys_multi
 *
 * Prepares keys with apid, ctid (ecuid:apid:ctid), will use ecuid if is provided
 * (ecuid:apid:ctid) or (:apid:ctid)
 *
 * @param ecuid          ECU ID
 * @param apid           Application ID
 * @param ctid           Context ID
 * @param key            Prepared key stored here
 * @return               None
 */
DLT_STATIC void dlt_logstorage_create_keys_multi(char *ecuid, char *apid,
                                                 char *ctid, char *key)
{
    char curr_str[DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN + 1] = { 0 };
    int curr_len = 0;

    if (ecuid != NULL) {
        strncpy(curr_str, ecuid, strlen(ecuid));
        strncat(curr_str, ":", 1);
    }
    else {
        strncpy(curr_str, ":", 1);
    }

    curr_len = strlen(apid);
    strncat(curr_str, apid, curr_len);
    strncat(curr_str, ":", 1);

    curr_len = strlen(ctid);
    strncat(curr_str, ctid, curr_len);
    curr_len = strlen(curr_str);

    strncpy(key, curr_str, curr_len);
}

/**
 * dlt_logstorage_create_keys_only_ecu
 *
 * Prepares keys with only ecuid (ecuid::)
 *
 * @param ecuid          ECU ID
 * @param key            Prepared key stored here
 * @return               None
 */
DLT_STATIC void dlt_logstorage_create_keys_only_ecu(char *ecuid, char *key)
{
    char curr_str[DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN + 1] = { 0 };

    strncpy(curr_str, ecuid, strlen(ecuid));
    strncat(curr_str, "::", 2);

    strncpy(key, curr_str, strlen(curr_str));
}

/**
 * dlt_logstorage_create_keys
 *
 * Create keys for hash table
 *
 * From each section [filter] in offline logstorage configuration file, we
 * receive application and context id strings.
 * Application and context id can consist of
 * - a 4char long name
 * - a comma separated list of ids
 * - a wildcard: .*
 *
 * Not allowed is the combination of application id and context id set to
 * wildcard. This will be rejected.
 *
 * If lists given for application and/or context id, all possible combinations
 * are returned as keys in a form "[apid][ctid], e.g. "APP1\:CTX1".
 * If wildcards are used, the non-wildcard value becomes the key, e.g. "APP1\:"
 * or "\:CTX2".
 *
 * @param[in] apids string given from filter configuration
 * @param[in] ctids string given from filter configuration
 * @param[in] ecuid string given from filter configuration
 * @param[out] keys keys to fill into hash table
 * @param[out] num_keys number of keys
 * @return: 0 on success, error on failure*
 */
DLT_STATIC int dlt_logstorage_create_keys(char *apids,
                                          char *ctids,
                                          char *ecuid,
                                          char **keys,
                                          int *num_keys)
{
    int i, j;
    int num_apids = 0;
    int num_ctids = 0;
    char *apid_list = NULL;
    char *ctid_list = NULL;
    char *curr_apid = NULL;
    char *curr_ctid = NULL;
    char curr_key[DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN + 1] = { 0 };
    int num_currkey = 0;

    /* Handle ecuid alone case here */
    if ((apids == NULL) && (ctids == NULL) && (ecuid != NULL)) {
        dlt_logstorage_create_keys_only_ecu(ecuid, curr_key);
        *(num_keys) = 1;
        *(keys) = (char *)calloc(*num_keys * DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN,
                                 sizeof(char));

        if (*(keys) == NULL)
            return -1;

        strncpy(*keys, curr_key, strlen(curr_key));
        return 0;
    }

    if ((apids == NULL) || (ctids == NULL)) {
        dlt_log(LOG_ERR, "Required inputs (apid and ctid) are NULL\n");
        return -1;
    }

    /* obtain key list and number of keys for application ids */
    if (dlt_logstorage_get_keys_list(apids, ",", &apid_list, &num_apids) != 0) {
        dlt_log(LOG_ERR, "Failed to obtain apid, check configuration file \n");
        return -1;
    }

    /* obtain key list and number of keys for context ids */
    if (dlt_logstorage_get_keys_list(ctids, ",", &ctid_list, &num_ctids) != 0) {
        dlt_log(LOG_ERR, "Failed to obtain ctid, check configuration file \n");
        free(apid_list);
        return -1;
    }

    *(num_keys) = num_apids * num_ctids;

    /* allocate memory for needed number of keys */
    *(keys) = (char *)calloc(*num_keys * DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN,
                             sizeof(char));

    if (*(keys) == NULL) {
        free(apid_list);
        free(ctid_list);
        return -1;
    }

    /* store all combinations of apid ctid in keys */
    for (i = 0; i < num_apids; i++) {
        curr_apid = apid_list + (i * (DLT_ID_SIZE + 1));

        for (j = 0; j < num_ctids; j++) {
            curr_ctid = ctid_list + (j * (DLT_ID_SIZE + 1));

            if (strncmp(curr_apid, ".*", 2) == 0) /* only context id matters */
                dlt_logstorage_create_keys_only_ctid(ecuid, curr_ctid, curr_key);
            else if (strncmp(curr_ctid, ".*", 2) == 0) /* only app id matters*/
                dlt_logstorage_create_keys_only_apid(ecuid, curr_apid, curr_key);
            else /* key is combination of all */
                dlt_logstorage_create_keys_multi(ecuid, curr_apid, curr_ctid, curr_key);

            strncpy((*keys + (num_currkey * DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN)),
                    curr_key, strlen(curr_key));
            num_currkey += 1;
            memset(&curr_key[0], 0, sizeof(curr_key));
        }
    }

    free(apid_list);
    free(ctid_list);

    return 0;
}

/**
 * dlt_logstorage_prepare_table
 *
 * Prepares hash table with keys and data
 *
 * @param handle         DLT Logstorage handle
 * @param data           Holds all other configuration values
 * @return               0 on success, -1 on error
 */
DLT_STATIC int dlt_logstorage_prepare_table(DltLogStorage *handle,
                                            DltLogStorageFilterConfig *data)
{
    int ret = 0;
    int num_keys = 0;
    char *keys = NULL;

    if ((handle == NULL) || (data == NULL)) {
        dlt_vlog(LOG_ERR, "Invalid parameters in %s\n", __func__);
        return -1;
    }

    ret = dlt_logstorage_create_keys(data->apids,
                                     data->ctids,
                                     data->ecuid,
                                     &keys,
                                     &num_keys);

    if (ret != 0) {
        dlt_log(LOG_ERR, "Not able to create keys for hash table\n");
        return -1;
    }

    /* hash_add */
    if (dlt_logstorage_list_add(keys,
                                num_keys,
                                data,
                                &(handle->config_list)) != 0)
    {
        dlt_log(LOG_ERR, "Adding to hash table failed, returning failure\n");
        dlt_logstorage_free(handle, DLT_LOGSTORAGE_SYNC_ON_ERROR);
        free(keys);
        keys = NULL;
        return -1;
    }

    free(keys);
    keys = NULL;
    return 0;
}

/**
 * dlt_logstorage_validate_filter_name
 *
 * Validates if the provided filter name is as required [FILTER<number>]
 *
 * @param name           Filter name
 * @return               0 on success, -1 on error
 *
 */
DLT_STATIC int dlt_logstorage_validate_filter_name(char *name)
{
    int len = 0;
    int idx = 0;
    int config_sec_len = strlen(DLT_OFFLINE_LOGSTORAGE_CONFIG_SECTION);
    int storage_sec_len = strlen(DLT_OFFLINE_LOGSTORAGE_NONVERBOSE_STORAGE_SECTION);
    int control_sec_len = strlen(DLT_OFFLINE_LOGSTORAGE_NONVERBOSE_CONTROL_SECTION);

    if (name == NULL)
        return -1;

    len = strlen(name);

    /* Check if section header is of format "FILTER" followed by a number */
    if (strncmp(name,
                DLT_OFFLINE_LOGSTORAGE_CONFIG_SECTION,
                config_sec_len) == 0) {
        for (idx = config_sec_len; idx < len - 1; idx++)
            if (!isdigit(name[idx]))
                return -1;

        return 0;
    }
    /* Check if section header is of format "FILTER" followed by a number */
    else if (strncmp(name,
                     DLT_OFFLINE_LOGSTORAGE_NONVERBOSE_STORAGE_SECTION,
                     storage_sec_len) == 0)
    {
        for (idx = storage_sec_len; idx < len - 1; idx++)
            if (!isdigit(name[idx]))
                return -1;

        return 0;
    }
    /* Check if section header is of format "FILTER" followed by a number */
    else if (strncmp(name,
                     DLT_OFFLINE_LOGSTORAGE_NONVERBOSE_CONTROL_SECTION,
                     control_sec_len) == 0)
    {
        for (idx = control_sec_len; idx < len - 1; idx++)
            if (!isdigit(name[idx]))
                return -1;

        return 0;
    }
    else {
        return -1;
    }
}

DLT_STATIC void dlt_logstorage_filter_set_strategy(DltLogStorageFilterConfig *config,
                                                   int strategy)
{
    if (config == NULL)
        return;

    /* file based */
    if ((strategy == DLT_LOGSTORAGE_SYNC_ON_MSG) ||
        (strategy == DLT_LOGSTORAGE_SYNC_UNSET)) {
        config->dlt_logstorage_prepare = &dlt_logstorage_prepare_on_msg;
        config->dlt_logstorage_write = &dlt_logstorage_write_on_msg;
        config->dlt_logstorage_sync = &dlt_logstorage_sync_on_msg;
    }
    else { /* cache based */
        config->dlt_logstorage_prepare = &dlt_logstorage_prepare_msg_cache;
        config->dlt_logstorage_write = &dlt_logstorage_write_msg_cache;
        config->dlt_logstorage_sync = &dlt_logstorage_sync_msg_cache;
    }
}

DLT_STATIC int dlt_logstorage_check_apids(DltLogStorageFilterConfig *config,
                                          char *value)
{
    if ((config == NULL) || (value == NULL)) {
        dlt_log(LOG_ERR, "Not able to create keys for hash table\n");
        return -1;
    }

    return dlt_logstorage_read_list_of_names(&config->apids, value);
}

DLT_STATIC int dlt_logstorage_check_ctids(DltLogStorageFilterConfig *config,
                                          char *value)
{
    if ((config == NULL) || (value == NULL))
        return -1;

    return dlt_logstorage_read_list_of_names(&config->ctids, value);
}

DLT_STATIC int dlt_logstorage_check_loglevel(DltLogStorageFilterConfig *config,
                                             char *value)
{
    if ((config == NULL) || (value == NULL))
        return -1;

    if (value == NULL) {
        config->log_level = 0;
        return -1;
    }

    if (strcmp(value, "DLT_LOG_FATAL") == 0) {
        config->log_level = 1;
    }
    else if (strcmp(value, "DLT_LOG_ERROR") == 0)
    {
        config->log_level = 2;
    }
    else if (strcmp(value, "DLT_LOG_WARN") == 0)
    {
        config->log_level = 3;
    }
    else if (strcmp(value, "DLT_LOG_INFO") == 0)
    {
        config->log_level = 4;
    }
    else if (strcmp(value, "DLT_LOG_DEBUG") == 0)
    {
        config->log_level = 5;
    }
    else if (strcmp(value, "DLT_LOG_VERBOSE") == 0)
    {
        config->log_level = 6;
    }
    else {
        config->log_level = -1;
        dlt_log(LOG_ERR, "Invalid log level \n");
        return -1;
    }

    return 0;
}

DLT_STATIC int dlt_logstorage_check_reset_loglevel(DltLogStorageFilterConfig *config,
                                                   char *value)
{
    if (config == NULL)
        return -1;

    if (value == NULL) {
        config->reset_log_level = 0;
        return -1;
    }

    if (strcmp(value, "DLT_LOG_OFF") == 0) {
        config->reset_log_level = DLT_LOG_OFF;
    }
    else if (strcmp(value, "DLT_LOG_FATAL") == 0)
    {
        config->reset_log_level = DLT_LOG_FATAL;
    }
    else if (strcmp(value, "DLT_LOG_ERROR") == 0)
    {
        config->reset_log_level = DLT_LOG_ERROR;
    }
    else if (strcmp(value, "DLT_LOG_WARN") == 0)
    {
        config->reset_log_level = DLT_LOG_WARN;
    }
    else if (strcmp(value, "DLT_LOG_INFO") == 0)
    {
        config->reset_log_level = DLT_LOG_INFO;
    }
    else if (strcmp(value, "DLT_LOG_DEBUG") == 0)
    {
        config->reset_log_level = DLT_LOG_DEBUG;
    }
    else if (strcmp(value, "DLT_LOG_VERBOSE") == 0)
    {
        config->reset_log_level = DLT_LOG_VERBOSE;
    }
    else {
        config->reset_log_level = -1;
        dlt_log(LOG_ERR, "Invalid log level \n");
        return -1;
    }

    return 0;
}

DLT_STATIC int dlt_logstorage_check_filename(DltLogStorageFilterConfig *config,
                                             char *value)
{
    int len;

    if ((value == NULL) || (strcmp(value, "") == 0))
        return -1;

    if (config->file_name != NULL) {
        free(config->file_name);
        config->file_name = NULL;
    }

    len = strlen(value);

    /* do not allow the user to change directory by adding a relative path */
    if (strstr(value, "..") == NULL) {
        config->file_name = calloc((len + 1), sizeof(char));

        if (config->file_name == NULL) {
            dlt_log(LOG_ERR,
                    "Cannot allocate memory for filename\n");
            return -1;
        }

        strncpy(config->file_name, value, len);
    }
    else {
        dlt_log(LOG_ERR,
                "Invalid filename, paths not accepted due to security issues\n");
        return -1;
    }

    return 0;
}

DLT_STATIC int dlt_logstorage_check_filesize(DltLogStorageFilterConfig *config,
                                             char *value)
{
    if ((config == NULL) || (value == NULL))
        return -1;

    return dlt_logstorage_read_number(&config->file_size, value);
}

DLT_STATIC int dlt_logstorage_check_nofiles(DltLogStorageFilterConfig *config,
                                            char *value)
{
    if ((config == NULL) || (value == NULL))
        return -1;

    return dlt_logstorage_read_number(&config->num_files, value);
}

DLT_STATIC int dlt_logstorage_check_specificsize(DltLogStorageFilterConfig *config,
                                                 char *value)
{
    if ((config == NULL) || (value == NULL))
        return -1;

    return dlt_logstorage_read_number(&config->specific_size, value);
}

/**
 * dlt_logstorage_check_sync_strategy
 *
 * Evaluate sync strategy. The sync strategy is an optional filter
 * configuration parameter.
 * If the given value cannot be associated with a sync strategy, the default
 * sync strategy will be assigned.
 *
 * @param config       DltLogStorageFilterConfig
 * @param value        string given in config file
 * @return             0 on success, -1 on error
 */
DLT_STATIC int dlt_logstorage_check_sync_strategy(DltLogStorageFilterConfig *config,
                                                  char *value)
{
    if ((config == NULL) || (value == NULL))
        return -1;

    if (strcasestr(value, "ON_MSG") != NULL) {
        config->sync = DLT_LOGSTORAGE_SYNC_ON_MSG;
        dlt_log(LOG_DEBUG, "ON_MSG found, ignore other if added\n");
    }
    else { /* ON_MSG not set, combination of cache based strategies possible */

        if (strcasestr(value, "ON_DAEMON_EXIT") != NULL)
            config->sync |= DLT_LOGSTORAGE_SYNC_ON_DAEMON_EXIT;

        if (strcasestr(value, "ON_DEMAND") != NULL)
            config->sync |= DLT_LOGSTORAGE_SYNC_ON_DEMAND;

        if (strcasestr(value, "ON_DEVICE_DISCONNECT") != NULL)
            config->sync |= DLT_LOGSTORAGE_SYNC_ON_DEVICE_DISCONNECT;

        if (strcasestr(value, "ON_SPECIFIC_SIZE") != NULL)
            config->sync |= DLT_LOGSTORAGE_SYNC_ON_SPECIFIC_SIZE;

        if (strcasestr(value, "ON_FILE_SIZE") != NULL)
            config->sync |= DLT_LOGSTORAGE_SYNC_ON_FILE_SIZE;

        if (config->sync == 0) {
            dlt_log(LOG_WARNING,
                    "Unknown sync strategies. Set default ON_MSG\n");
            config->sync = DLT_LOGSTORAGE_SYNC_ON_MSG;
            return 1;
        }
    }

    return 0;
}

/**
 * dlt_logstorage_check_ecuid
 *
 * Evaluate if ECU idenfifier given in config file
 *
 * @param config       DltLogStorageFilterConfig
 * @param value        string given in config file
 * @return             0 on success, -1 on error
 */
DLT_STATIC int dlt_logstorage_check_ecuid(DltLogStorageFilterConfig *config,
                                          char *value)
{
    int len;

    if ((config == NULL) || (value == NULL) || (value[0] == '\0'))
        return -1;

    if (config->ecuid != NULL) {
        free(config->ecuid);
        config->ecuid = NULL;
    }

    len = strlen(value);
    config->ecuid = calloc((len + 1), sizeof(char));

    if (config->ecuid == NULL)
        return -1;

    strncpy(config->ecuid, value, len);

    return 0;
}

DLT_STATIC DltLogstorageFilterConf
    filter_cfg_entries[DLT_LOGSTORAGE_FILTER_CONF_COUNT] = {
    [DLT_LOGSTORAGE_FILTER_CONF_LOGAPPNAME] = {
        .key = "LogAppName",
        .func = dlt_logstorage_check_apids,
        .is_opt = 1
    },
    [DLT_LOGSTORAGE_FILTER_CONF_CONTEXTNAME] = {
        .key = "ContextName",
        .func = dlt_logstorage_check_ctids,
        .is_opt = 1
    },
    [DLT_LOGSTORAGE_FILTER_CONF_LOGLEVEL] = {
        .key = "LogLevel",
        .func = dlt_logstorage_check_loglevel,
        .is_opt = 0
    },
    [DLT_LOGSTORAGE_FILTER_CONF_RESET_LOGLEVEL] = {
        .key = NULL,
        .func = dlt_logstorage_check_reset_loglevel,
        .is_opt = 0
    },
    [DLT_LOGSTORAGE_FILTER_CONF_FILE] = {
        .key = "File",
        .func = dlt_logstorage_check_filename,
        .is_opt = 0
    },
    [DLT_LOGSTORAGE_FILTER_CONF_FILESIZE] = {
        .key = "FileSize",
        .func = dlt_logstorage_check_filesize,
        .is_opt = 0
    },
    [DLT_LOGSTORAGE_FILTER_CONF_NOFILES] = {
        .key = "NOFiles",
        .func = dlt_logstorage_check_nofiles,
        .is_opt = 0
    },
    [DLT_LOGSTORAGE_FILTER_CONF_SYNCBEHAVIOR] = {
        .key = "SyncBehavior",
        .func = dlt_logstorage_check_sync_strategy,
        .is_opt = 1
    },
    [DLT_LOGSTORAGE_FILTER_CONF_ECUID] = {
        .key = "EcuID",
        .func = dlt_logstorage_check_ecuid,
        .is_opt = 1
    },
    [DLT_LOGSTORAGE_FILTER_CONF_SPECIFIC_SIZE] = {
        .key = "SpecificSize",
        .func = dlt_logstorage_check_specificsize,
        .is_opt = 1
    }
};

/* */
DLT_STATIC DltLogstorageFilterConf
    filter_nonverbose_storage_entries[DLT_LOGSTORAGE_FILTER_CONF_COUNT] = {
    [DLT_LOGSTORAGE_FILTER_CONF_LOGAPPNAME] = {
        .key = NULL,
        .func = dlt_logstorage_check_apids,
        .is_opt = 0
    },
    [DLT_LOGSTORAGE_FILTER_CONF_CONTEXTNAME] = {
        .key = NULL,
        .func = dlt_logstorage_check_ctids,
        .is_opt = 0
    },
    [DLT_LOGSTORAGE_FILTER_CONF_LOGLEVEL] = {
        .key = NULL,
        .func = dlt_logstorage_check_loglevel,
        .is_opt = 0
    },
    [DLT_LOGSTORAGE_FILTER_CONF_RESET_LOGLEVEL] = {
        .key = NULL,
        .func = NULL,
        .is_opt = 0
    },
    [DLT_LOGSTORAGE_FILTER_CONF_FILE] = {
        .key = "File",
        .func = dlt_logstorage_check_filename,
        .is_opt = 0
    },
    [DLT_LOGSTORAGE_FILTER_CONF_FILESIZE] = {
        .key = "FileSize",
        .func = dlt_logstorage_check_filesize,
        .is_opt = 0
    },
    [DLT_LOGSTORAGE_FILTER_CONF_NOFILES] = {
        .key = "NOFiles",
        .func = dlt_logstorage_check_nofiles,
        .is_opt = 0
    },
    [DLT_LOGSTORAGE_FILTER_CONF_SYNCBEHAVIOR] = {
        .key = NULL,
        .func = dlt_logstorage_check_sync_strategy,
        .is_opt = 1
    },
    [DLT_LOGSTORAGE_FILTER_CONF_ECUID] = {
        .key = "EcuID",
        .func = dlt_logstorage_check_ecuid,
        .is_opt = 0
    },
    [DLT_LOGSTORAGE_FILTER_CONF_SPECIFIC_SIZE] = {
        .key = NULL,
        .func = dlt_logstorage_check_specificsize,
        .is_opt = 1
    }
};

DLT_STATIC DltLogstorageFilterConf
    filter_nonverbose_control_entries[DLT_LOGSTORAGE_FILTER_CONF_COUNT] = {
    [DLT_LOGSTORAGE_FILTER_CONF_LOGAPPNAME] = {
        .key = "LogAppName",
        .func = dlt_logstorage_check_apids,
        .is_opt = 0
    },
    [DLT_LOGSTORAGE_FILTER_CONF_CONTEXTNAME] = {
        .key = "ContextName",
        .func = dlt_logstorage_check_ctids,
        .is_opt = 0
    },
    [DLT_LOGSTORAGE_FILTER_CONF_LOGLEVEL] = {
        .key = "LogLevel",
        .func = dlt_logstorage_check_loglevel,
        .is_opt = 0
    },
    [DLT_LOGSTORAGE_FILTER_CONF_RESET_LOGLEVEL] = {
        .key = "ResetLogLevel",
        .func = dlt_logstorage_check_reset_loglevel,
        .is_opt = 1
    },
    [DLT_LOGSTORAGE_FILTER_CONF_FILE] = {
        .key = NULL,
        .func = dlt_logstorage_check_filename,
        .is_opt = 0
    },
    [DLT_LOGSTORAGE_FILTER_CONF_FILESIZE] = {
        .key = NULL,
        .func = dlt_logstorage_check_filesize,
        .is_opt = 0
    },
    [DLT_LOGSTORAGE_FILTER_CONF_NOFILES] = {
        .key = NULL,
        .func = dlt_logstorage_check_nofiles,
        .is_opt = 0
    },
    [DLT_LOGSTORAGE_FILTER_CONF_SYNCBEHAVIOR] = {
        .key = NULL,
        .func = dlt_logstorage_check_sync_strategy,
        .is_opt = 1
    },
    [DLT_LOGSTORAGE_FILTER_CONF_ECUID] = {
        .key = "EcuID",
        .func = dlt_logstorage_check_ecuid,
        .is_opt = 0
    },
    [DLT_LOGSTORAGE_FILTER_CONF_SPECIFIC_SIZE] = {
        .key = NULL,
        .func = dlt_logstorage_check_specificsize,
        .is_opt = 1
    }
};
/**
 * Check filter configuration parameter is valid.
 *
 * @param config DltLogStorageFilterConfig
 * @param ctype  DltLogstorageFilterConfType
 * @param value specified property value from configuration file
 * @return 0 on success, -1 otherwise
 */
DLT_STATIC int dlt_logstorage_check_param(DltLogStorageFilterConfig *config,
                                          DltLogstorageFilterConfType ctype,
                                          char *value)
{
    if ((config == NULL) || (value == NULL))
        return -1;

    if (ctype < DLT_LOGSTORAGE_FILTER_CONF_COUNT)
        return filter_cfg_entries[ctype].func(config, value);

    return -1;
}

DLT_STATIC int dlt_logstorage_get_filter_value(DltConfigFile *config_file,
                                               char *sec_name,
                                               int index,
                                               char *value)
{
    int ret = 0;
    int config_sec_len = strlen(DLT_OFFLINE_LOGSTORAGE_CONFIG_SECTION);
    int storage_sec_len = strlen(DLT_OFFLINE_LOGSTORAGE_NONVERBOSE_STORAGE_SECTION);
    int control_sec_len = strlen(DLT_OFFLINE_LOGSTORAGE_NONVERBOSE_CONTROL_SECTION);

    if ((config_file == NULL) || (sec_name == NULL))
        return DLT_OFFLINE_LOGSTORAGE_FILTER_ERROR;

    /* Branch based on section name, no complete string compare needed */
    if (strncmp(sec_name,
                DLT_OFFLINE_LOGSTORAGE_CONFIG_SECTION,
                config_sec_len) == 0) {
        if (filter_cfg_entries[index].key != NULL) {
            ret = dlt_config_file_get_value(config_file, sec_name,
                                            filter_cfg_entries[index].key,
                                            value);

            if ((ret != 0) && (filter_cfg_entries[index].is_opt == 0)) {
                dlt_vlog(LOG_WARNING,
                         "Invalid configuration in section: %s -> %s : %s\n",
                         sec_name, filter_cfg_entries[index].key, value);
                return DLT_OFFLINE_LOGSTORAGE_FILTER_ERROR;
            }

            if ((ret != 0) && (filter_cfg_entries[index].is_opt == 1)) {
                dlt_vlog(LOG_DEBUG, "Optional parameter %s not specified\n",
                         filter_cfg_entries[index].key);
                return DLT_OFFLINE_LOGSTORAGE_FILTER_CONTINUE;
            }
        }
        else {
            return DLT_OFFLINE_LOGSTORAGE_FILTER_CONTINUE;
        }
    }
    else if (strncmp(sec_name,
                     DLT_OFFLINE_LOGSTORAGE_NONVERBOSE_STORAGE_SECTION,
                     storage_sec_len) == 0)
    {
        if (filter_nonverbose_storage_entries[index].key != NULL) {
            if (dlt_config_file_get_value(config_file, sec_name,
                                          filter_nonverbose_storage_entries[index].key, value) != 0) {
                dlt_vlog(LOG_WARNING,
                         "Invalid configuration in section: %s -> %s : %s\n",
                         sec_name, filter_nonverbose_storage_entries[index].key,
                         value);
                return DLT_OFFLINE_LOGSTORAGE_FILTER_ERROR;
            }
        }
        else {
            return DLT_OFFLINE_LOGSTORAGE_FILTER_CONTINUE;
        }
    }
    else if ((strncmp(sec_name,
                      DLT_OFFLINE_LOGSTORAGE_NONVERBOSE_CONTROL_SECTION,
                      control_sec_len) == 0))
    {
        if (filter_nonverbose_control_entries[index].key != NULL) {
            if (dlt_config_file_get_value(config_file, sec_name,
                                          filter_nonverbose_control_entries[index].key, value) != 0) {
                dlt_vlog(LOG_WARNING,
                         "Invalid configuration in section: %s -> %s : %s\n",
                         sec_name, filter_nonverbose_control_entries[index].key,
                         value);
                return DLT_OFFLINE_LOGSTORAGE_FILTER_ERROR;
            }
        }
        else {
            return DLT_OFFLINE_LOGSTORAGE_FILTER_CONTINUE;
        }
    }
    else {
        dlt_log(LOG_ERR, "Error: Section name not valid \n");
        return DLT_OFFLINE_LOGSTORAGE_FILTER_ERROR;
    }

    return 0;
}

DLT_STATIC int dlt_logstorage_setup_table(DltLogStorage *handle,
                                          DltLogStorageFilterConfig *tmp_data)
{
    int ret = 0;

    /* depending on the specified strategy set function pointers for
     * prepare, write and sync */
    dlt_logstorage_filter_set_strategy(tmp_data, tmp_data->sync);

    ret = dlt_logstorage_prepare_table(handle, tmp_data);

    if (ret != 0) {
        dlt_vlog(LOG_ERR, "%s Error: Storing filter values failed\n", __func__);
        ret = DLT_OFFLINE_LOGSTORAGE_STORE_FILTER_ERROR;
    }

    return ret;
}
/*Return :
 * DLT_OFFLINE_LOGSTORAGE_FILTER_ERROR - On filter properties or value is not valid
 * DLT_OFFLINE_LOGSTORAGE_STORE_FILTER_ERROR - On error while storing in hash table
 */

DLT_STATIC int dlt_daemon_offline_setup_filter_properties(DltLogStorage *handle,
                                                          DltConfigFile *config_file,
                                                          char *sec_name)
{
    DltLogStorageFilterConfig tmp_data;
    char value[DLT_CONFIG_FILE_ENTRY_MAX_LEN + 1] = { '\0' };
    int i = 0;
    int ret = 0;

    if ((handle == NULL) || (config_file == NULL) || (sec_name == NULL))
        return DLT_OFFLINE_LOGSTORAGE_STORE_FILTER_ERROR;

    memset(&tmp_data, 0, sizeof(DltLogStorageFilterConfig));
    tmp_data.apids = NULL;
    tmp_data.ctids = NULL;
    tmp_data.file_name = NULL;
    tmp_data.ecuid = NULL;
    tmp_data.log_level = DLT_LOG_VERBOSE;
    tmp_data.reset_log_level = DLT_LOG_OFF;
    tmp_data.records = NULL;
    tmp_data.log = NULL;
    tmp_data.cache = NULL;

    for (i = 0; i < DLT_LOGSTORAGE_FILTER_CONF_COUNT; i++) {
        ret = dlt_logstorage_get_filter_value(config_file, sec_name, i, value);

        if (ret == DLT_OFFLINE_LOGSTORAGE_FILTER_ERROR)
            return ret;

        if (ret == DLT_OFFLINE_LOGSTORAGE_FILTER_CONTINUE)
            continue;

        /* check value and store temporary */
        ret = dlt_logstorage_check_param(&tmp_data, i, value);

        if (ret != 0) {
            if (tmp_data.apids != NULL) {
                free(tmp_data.apids);
                tmp_data.apids = NULL;
            }

            if (tmp_data.ctids != NULL) {
                free(tmp_data.ctids);
                tmp_data.ctids = NULL;
            }

            if (tmp_data.file_name != NULL) {
                free(tmp_data.file_name);
                tmp_data.file_name = NULL;
            }

            if (tmp_data.ecuid != NULL) {
                free(tmp_data.ecuid);
                tmp_data.ecuid = NULL;
            }

            return DLT_OFFLINE_LOGSTORAGE_FILTER_ERROR;
        }
    }

    /* filter configuration is valid */
    ret = dlt_logstorage_setup_table(handle, &tmp_data);

    if (ret != 0) {
        dlt_vlog(LOG_ERR, "%s Error: Storing filter values failed\n", __func__);
        ret = DLT_OFFLINE_LOGSTORAGE_STORE_FILTER_ERROR;
    }
    else { /* move to next free filter configuration, if no error occurred */
        handle->num_configs += 1;
    }

    /* free tmp_data */
    dlt_logstorage_filter_config_free(&tmp_data);

    return ret;
}

/**
 * dlt_logstorage_store_filters
 *
 * This function reads the filter keys and values
 * and stores them into the hash map
 *
 * @param handle             DLT Logstorage handle
 * @param config_file_name   Configuration file name
 * @return                   0 on success, -1 on error, 1 on warning
 *
 */
DLT_STATIC int dlt_logstorage_store_filters(DltLogStorage *handle,
                                            char *config_file_name)
{
    DltConfigFile *config = NULL;
    int sec = 0;
    int num_sec = 0;
    int ret = 0;
    /* we have to make sure that this function returns success if atleast one
     * filter configuration is valid and stored */
    int valid = -1;

    if (config_file_name == NULL) {
        dlt_vlog(LOG_ERR, "%s unexpected parameter received\n", __func__);
        return -1;
    }

    config = dlt_config_file_init(config_file_name);

    if (config == NULL) {
        dlt_log(LOG_CRIT, "Failed to open filter configuration file\n");
        return -1;
    }

    dlt_config_file_get_num_sections(config, &num_sec);

    for (sec = 0; sec < num_sec; sec++) {
        char sec_name[DLT_CONFIG_FILE_ENTRY_MAX_LEN + 1];

        if (dlt_config_file_get_section_name(config, sec, sec_name) == -1) {
            dlt_log(LOG_CRIT, "Failed to read section name\n");
            dlt_config_file_release(config);
            return -1;
        }

        if (strstr(sec_name, GENERAL_BASE_NAME) != NULL) {
            dlt_log(LOG_CRIT, "General configuration not supported \n");
            continue;
        }
        else if (dlt_logstorage_validate_filter_name(sec_name) == 0)
        {
            ret = dlt_daemon_offline_setup_filter_properties(handle, config, sec_name);

            if (ret == DLT_OFFLINE_LOGSTORAGE_STORE_FILTER_ERROR) {
                break;
            }
            else if (ret == DLT_OFFLINE_LOGSTORAGE_FILTER_ERROR)
            {
                valid = 1;
                dlt_vlog(LOG_WARNING,
                         "%s filter configuration is invalid \n",
                         sec_name);
                /* Continue reading next filter section */
                continue;
            }
            else
            /* Filter properties read and stored successfuly */
            if (valid != 1)
                valid = 0;
        }
        else { /* unknown section */
            dlt_vlog(LOG_WARNING, "Unknown section: %s", sec_name);
        }
    }

    dlt_config_file_release(config);

    return valid;
}

/**
 * dlt_logstorage_load_config
 *
 * Read dlt_logstorage.conf file and setup filters in hash table
 * Hash table key consists of "APID:CTID", e.g "APP1:CTX1". If
 * wildcards used for application id or context id, the hash table
 * key consists of none wildcard value, e.g. apid=.*, cxid=CTX1
 * results in "CTX1".
 *
 * Combination of two wildcards is not allowed.
 *
 * @param handle        DLT Logstorage handle
 * @return              0 on success, -1 on error, 1 on warning
 */
DLT_STATIC int dlt_logstorage_load_config(DltLogStorage *handle)
{
    char config_file_name[PATH_MAX] = {0};
    int ret = 0;

    /* Check if handle is NULL or already initialized or already configured  */
    if ((handle == NULL) ||
        (handle->connection_type != DLT_OFFLINE_LOGSTORAGE_DEVICE_CONNECTED))
        return -1;

    /* Check if this device config was already setup */
    if (handle->config_status == DLT_OFFLINE_LOGSTORAGE_CONFIG_DONE) {
        dlt_vlog(LOG_ERR,
                 "%s: Device already configured. Send disconnect first.\n",
                 __func__);
        return -1;
    }

    if (snprintf(config_file_name,
                 PATH_MAX,
                 "%s/%s",
                 handle->device_mount_point,
                 DLT_OFFLINE_LOGSTORAGE_CONFIG_FILE_NAME) < 0) {
        dlt_log(LOG_ERR,
                "Creating configuration file path string failed\n");
        return -1;
    }
    config_file_name[PATH_MAX - 1] = 0;
    ret = dlt_logstorage_store_filters(handle, config_file_name);

    if (ret == 1) {
        handle->config_status = DLT_OFFLINE_LOGSTORAGE_CONFIG_DONE;
        return 1;
    }
    else if (ret != 0)
    {
        dlt_log(LOG_ERR,
                "dlt_logstorage_load_config Error : Storing filters failed\n");
        return -1;
    }

    handle->config_status = DLT_OFFLINE_LOGSTORAGE_CONFIG_DONE;

    return 0;
}

/**
 * dlt_logstorage_device_connected
 *
 * Initializes DLT Offline Logstorage with respect to device status
 *
 * @param handle         DLT Logstorage handle
 * @param mount_point    Device mount path
 * @return               0 on success, -1 on error, 1 on warning
 */
int dlt_logstorage_device_connected(DltLogStorage *handle, char *mount_point)
{
    if ((handle == NULL) || (mount_point == NULL)) {
        dlt_log(LOG_ERR, "Handle error \n");
        return -1;
    }

    if (handle->connection_type == DLT_OFFLINE_LOGSTORAGE_DEVICE_CONNECTED) {
        dlt_log(LOG_WARNING,
                "Device already connected. Send disconnect, connect request\n");

        dlt_logstorage_device_disconnected(
            handle,
            DLT_LOGSTORAGE_SYNC_ON_DEVICE_DISCONNECT);
    }

    strncpy(handle->device_mount_point, mount_point, DLT_MOUNT_PATH_MAX);
    handle->device_mount_point[DLT_MOUNT_PATH_MAX] = 0;
    handle->connection_type = DLT_OFFLINE_LOGSTORAGE_DEVICE_CONNECTED;
    handle->config_status = 0;
    handle->write_errors = 0;
    handle->num_configs = 0;

    /* Setup logstorage with config file settings */
    return dlt_logstorage_load_config(handle);
}

/**
 * dlt_logstorage_device_disconnected
 *
 * De-Initializes DLT Offline Logstorage with respect to device status
 *
 * @param handle         DLT Logstorage handle
 * @param reason         Reason for disconnect
 * @return               0 on success, -1 on error
 *
 */
int dlt_logstorage_device_disconnected(DltLogStorage *handle, int reason)
{
    if (handle == NULL)
        return -1;

    /* If configuration loading was done, free it */
    if (handle->config_status == DLT_OFFLINE_LOGSTORAGE_CONFIG_DONE)
        dlt_logstorage_free(handle, reason);

    /* Reset all device status */
    memset(handle->device_mount_point, 0, sizeof(char) * (DLT_MOUNT_PATH_MAX + 1));
    handle->connection_type = DLT_OFFLINE_LOGSTORAGE_DEVICE_DISCONNECTED;
    handle->config_status = 0;
    handle->write_errors = 0;
    handle->num_configs = 0;

    return 0;
}

/**
 * dlt_logstorage_get_loglevel_by_key
 *
 * Obtain the log level for the provided key
 * This function can be used to obtain log level when the actual
 * key stored in the Hash map is availble with the caller
 *
 * @param handle    DltLogstorage handle
 * @param key       key to search for in Hash MAP
 * @return          log level on success:, -1 on error
 */
int dlt_logstorage_get_loglevel_by_key(DltLogStorage *handle, char *key)
{
    DltLogStorageFilterConfig *config[DLT_CONFIG_FILE_SECTIONS_MAX] = { 0 };
    int num_configs = 0;
    int i = 0;
    int log_level = 0;

    /* Check if handle is NULL,already initialized or already configured  */
    if ((handle == NULL) ||
        (key == NULL) ||
        (handle->connection_type != DLT_OFFLINE_LOGSTORAGE_DEVICE_CONNECTED) ||
        (handle->config_status != DLT_OFFLINE_LOGSTORAGE_CONFIG_DONE))
        return -1;

    num_configs = dlt_logstorage_list_find(key, &(handle->config_list), config);

    if (num_configs == 0)
    {
        dlt_vlog(LOG_WARNING, "Configuration for key [%s] not found!\n", key);
        return -1;
    }
    else if (num_configs == 1)
    {
        if (config[0] != NULL)
        {
            log_level = config[0]->log_level;
        }
    }
    else
    {
        /**
         * Multiple configurations found, raise a warning to the user and go
         * for the more verbose one.
         */
        dlt_vlog(LOG_WARNING, "Multiple configuration for key [%s] found,"
                 " return the highest log level!\n", key);

        for (i = 0; i < num_configs; i++)
        {
            if ((config[i] != NULL) && (config[i]->log_level > log_level))
            {
                log_level = config[i]->log_level;
            }
        }
    }

    return log_level;
}

/**
 * dlt_logstorage_get_config
 *
 * Obtain the configuration data of all filters for provided apid and ctid
 *
 * @param handle    DltLogStorage handle
 * @param config    [out] Pointer to array of filter configurations
 * @param apid      application id
 * @param ctid      context id
 * @param ecuid     ecu id
 * @return          number of configurations found
 */
int dlt_logstorage_get_config(DltLogStorage *handle,
                              DltLogStorageFilterConfig **config,
                              char *apid,
                              char *ctid,
                              char *ecuid)
{
    DltLogStorageFilterConfig **cur_config_ptr = NULL;
    char key[DLT_CONFIG_FILE_SECTIONS_MAX][DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN] =
    { { '\0' }, { '\0' }, { '\0' } };
    int i = 0;
    int apid_len = 0;
    int ctid_len = 0;
    int ecuid_len = 0;
    int num_configs = 0;
    int num = 0;

    /* Check if handle is NULL,already initialized or already configured  */
    if ((handle == NULL) || (config == NULL) ||
        (handle->connection_type != DLT_OFFLINE_LOGSTORAGE_DEVICE_CONNECTED) ||
        (handle->config_status != DLT_OFFLINE_LOGSTORAGE_CONFIG_DONE) ||
        (ecuid == NULL))
        return 0;

    /* Prepare possible keys with
     * Possible combinations are
     * ecu::
     * ecu:apid:ctid
     * :apid:ctid
     * ecu::ctid
     * ecu:apid:
     * ::ctid
     * :apid: */

    ecuid_len = strlen(ecuid);

    if (ecuid_len > DLT_ID_SIZE)
        ecuid_len = DLT_ID_SIZE;

    if ((apid == NULL) && (ctid == NULL)) {
        /* ecu:: */
        strncpy(key[0], ecuid, ecuid_len);
        strncat(key[0], ":", 1);
        strncat(key[0], ":", 1);

        num_configs = dlt_logstorage_list_find(key[0], &(handle->config_list),
                                               config);
        return num_configs;
    }

    apid_len = strlen(apid);

    if (apid_len > DLT_ID_SIZE)
        apid_len = DLT_ID_SIZE;

    ctid_len = strlen(ctid);

    if (ctid_len > DLT_ID_SIZE)
        ctid_len = DLT_ID_SIZE;

    /* :apid: */
    strncpy(key[0], ":", 1);
    strncat(key[0], apid, apid_len);
    strncat(key[0], ":", 1);

    /* ::ctid */
    strncpy(key[1], ":", 1);
    strncat(key[1], ":", 1);
    strncat(key[1], ctid, ctid_len);

    /* :apid:ctid */
    strncpy(key[2], ":", 1);
    strncat(key[2], apid, apid_len);
    strncat(key[2], ":", 1);
    strncat(key[2], ctid, ctid_len);

    /* ecu:apid:ctid */
    strncpy(key[3], ecuid, ecuid_len);
    strncat(key[3], ":", 1);
    strncat(key[3], apid, apid_len);
    strncat(key[3], ":", 1);
    strncat(key[3], ctid, ctid_len);

    /* ecu:apid: */
    strncpy(key[4], ecuid, ecuid_len);
    strncat(key[4], ":", 1);
    strncat(key[4], apid, apid_len);
    strncat(key[4], ":", 1);

    /* ecu::ctid */
    strncpy(key[5], ecuid, ecuid_len);
    strncat(key[5], ":", 1);
    strncat(key[5], ":", 1);
    strncat(key[5], ctid, ctid_len);

    /* ecu:: */
    strncpy(key[6], ecuid, ecuid_len);
    strncat(key[6], ":", 1);
    strncat(key[6], ":", 1);

    /* Search the list three times with keys as -apid: , :ctid and apid:ctid */
    for (i = 0; i < DLT_OFFLINE_LOGSTORAGE_MAX_POSSIBLE_KEYS; i++)
    {
        cur_config_ptr = &config[num_configs];
        num = dlt_logstorage_list_find(key[i], &(handle->config_list),
                                       cur_config_ptr);
        num_configs += num;
        /* If all filter configurations matched, stop and return */
        if (num_configs == handle->num_configs)
        {
            break;
        }
    }

    return num_configs;
}

/**
 * dlt_logstorage_filter
 *
 * Check if log message need to be stored in a certain device based on filter
 * config
 * - get all DltLogStorageFilterConfig from hash table possible by given
 *   apid/ctid (apid:, :ctid, apid:ctid
 * - for each found structure, compare message log level with configured one
 *
 * @param handle    DltLogStorage handle
 * @param config    Pointer to array of filter configurations
 * @param apid      application id
 * @param ctid      context id
 * @param log_level Log level of message
 * @param ecuid     EcuID given in the message
 * @return          number of found configurations
 */
DLT_STATIC int dlt_logstorage_filter(DltLogStorage *handle,
                                     DltLogStorageFilterConfig **config,
                                     char *apid,
                                     char *ctid,
                                     char *ecuid,
                                     int log_level)
{
    int i = 0;
    int num = 0;

    if ((handle == NULL) || (config == NULL) || (ecuid == NULL))
        return -1;

    /* filter on names: find DltLogStorageFilterConfig structures */
    num = dlt_logstorage_get_config(handle, config, apid, ctid, ecuid);

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

    for (i = 0 ; i < num ; i++)
    {
        if (config[i] == NULL)
            continue;

        /* filter on log level */
        if (log_level > config[i]->log_level) {
            config[i] = NULL;
            continue;
        }

        /* filter on ECU id only if EcuID is set */
        if (config[i]->ecuid != NULL) {
            if (strncmp(ecuid, config[i]->ecuid, DLT_ID_SIZE) != 0)
                config[i] = NULL;
        }
    }

    return num;
}

/**
 * dlt_logstorage_write
 *
 * Write a message to one or more configured log files, based on filter
 * configuration.
 *
 * @param handle    DltLogStorage handle
 * @param uconfig    User configurations for log file
 * @param data1     Data buffer of message header
 * @param size1     Size of message header buffer
 * @param data2     Data buffer of extended message body
 * @param size2     Size of extended message body
 * @param data3     Data buffer of message body
 * @param size3     Size of message body
 * @return          0 on success or write errors < max write errors, -1 on error
 */
int dlt_logstorage_write(DltLogStorage *handle,
                         DltLogStorageUserConfig *uconfig,
                         unsigned char *data1,
                         int size1,
                         unsigned char *data2,
                         int size2,
                         unsigned char *data3,
                         int size3)
{
    DltLogStorageFilterConfig *config[DLT_CONFIG_FILE_SECTIONS_MAX] = { 0 };

    int i = 0;
    int ret = 0;
    int num = 0;
    int err = 0;
    /* data2 contains DltStandardHeader, DltStandardHeaderExtra and
     * DltExtendedHeader. We are interested in ecuid, apid, ctid and loglevel */
    DltExtendedHeader *extendedHeader = NULL;
    DltStandardHeaderExtra *extraHeader = NULL;
    DltStandardHeader *standardHeader = NULL;
    unsigned int standardHeaderExtraLen = sizeof(DltStandardHeaderExtra);
    unsigned int header_len = 0;

    int log_level = -1;

    if ((handle == NULL) || (uconfig == NULL) ||
        (data1 == NULL) || (data2 == NULL) || (data3 == NULL) ||
        (handle->connection_type != DLT_OFFLINE_LOGSTORAGE_DEVICE_CONNECTED) ||
        (handle->config_status != DLT_OFFLINE_LOGSTORAGE_CONFIG_DONE))
        return 0;

    /* Calculate real length of DltStandardHeaderExtra */
    standardHeader = (DltStandardHeader *)data2;

    if (!DLT_IS_HTYP_WEID(standardHeader->htyp))
        standardHeaderExtraLen -= DLT_ID_SIZE;

    if (!DLT_IS_HTYP_WSID(standardHeader->htyp))
        standardHeaderExtraLen -= DLT_SIZE_WSID;

    if (!DLT_IS_HTYP_WTMS(standardHeader->htyp))
        standardHeaderExtraLen -= DLT_SIZE_WTMS;

    extraHeader = (DltStandardHeaderExtra *)(data2
                                             + sizeof(DltStandardHeader));

    if (DLT_IS_HTYP_UEH(standardHeader->htyp)) {
        header_len = sizeof(DltStandardHeader) + sizeof(DltExtendedHeader) + standardHeaderExtraLen;

        /* check if size2 is big enough to contain expected DLT message header */
        if ((unsigned int)size2 < header_len) {
            dlt_log(LOG_ERR, "DLT message header is too small\n");
            return 0;
        }

        extendedHeader = (DltExtendedHeader *)(data2
                                               + sizeof(DltStandardHeader) + standardHeaderExtraLen);

        log_level = DLT_GET_MSIN_MTIN(extendedHeader->msin);

        /* check if log message need to be stored in a certain device based on
         * filter configuration */
        num = dlt_logstorage_filter(handle, config, extendedHeader->apid,
                                    extendedHeader->ctid, extraHeader->ecu, log_level);

        if ((num == 0) || (num == -1)) {
            dlt_log(LOG_DEBUG, "No valid filter configuration found!\n");
            return 0;
        }
    }
    else {
        header_len = sizeof(DltStandardHeader) + standardHeaderExtraLen;

        /* check if size2 is big enough to contain expected DLT message header */
        if ((unsigned int)size2 < header_len) {
            dlt_log(LOG_ERR, "DLT message header is too small (without extended header)\n");
            return 0;
        }

        log_level = DLT_LOG_VERBOSE;

        /* check if log message need to be stored in a certain device based on
         * filter configuration */
        num = dlt_logstorage_filter(handle, config, NULL,
                                    NULL, extraHeader->ecu, log_level);

        if ((num == 0) || (num == -1)) {
            dlt_log(LOG_DEBUG, "No valid filter configuration found!\n");
            return 0;
        }
    }

    /* store log message in every found filter */
    for (i = 0; i < num; i++)
    {
        if (config[i] == NULL)
            continue;

        /* If file name is not present, the filter is non verbose control filter
         * hence skip storing */
        if (config[i]->file_name == NULL)
            continue;

        /* prepare log file (create and/or open)*/
        ret = config[i]->dlt_logstorage_prepare(config[i],
                                                uconfig,
                                                handle->device_mount_point,
                                                size1 + size2 + size3);

        if (ret == 0) { /* log data (write) */
            ret = config[i]->dlt_logstorage_write(config[i],
                                                  uconfig,
                                                  handle->device_mount_point,
                                                  data1,
                                                  size1,
                                                  data2,
                                                  size2,
                                                  data3,
                                                  size3);

            if (ret == 0) {
                /* flush to be sure log is stored on device */
                ret = config[i]->dlt_logstorage_sync(config[i],
                                                     uconfig,
                                                     handle->device_mount_point,
                                                     DLT_LOGSTORAGE_SYNC_ON_MSG);

                if (ret != 0)
                    dlt_log(LOG_ERR,
                            "dlt_logstorage_write: Unable to sync.\n");
            }
            else {
                handle->write_errors += 1;

                if (handle->write_errors >=
                    DLT_OFFLINE_LOGSTORAGE_MAX_WRITE_ERRORS)
                    err = -1;

                dlt_log(LOG_ERR,
                        "dlt_logstorage_write: Unable to write.\n");
            }
        }
        else {
            dlt_log(LOG_ERR,
                    "dlt_logstorage_write: Unable to prepare.\n");
        }
    }

    return err;
}

/**
 * dlt_logstorage_sync_caches
 *
 * Write Cache data to file
 *
 * @param handle     DltLogStorage handle
 * @return           0 on success, -1 on error
 */
int dlt_logstorage_sync_caches(DltLogStorage *handle)
{
    DltLogStorageFilterList **tmp = NULL;

    if (handle == NULL)
        return -1;

    tmp = &(handle->config_list);

    while (*(tmp) != NULL) {
        if ((*tmp)->data != NULL) {
            if ((*tmp)->data->dlt_logstorage_sync((*tmp)->data,
                                                  &handle->uconfig,
                                                  handle->device_mount_point,
                                                  DLT_LOGSTORAGE_SYNC_ON_DEMAND) != 0)
                dlt_vlog(LOG_ERR,
                         "%s: Sync failed. Continue with next cache.\n",
                         __func__);
        }

        tmp = &(*tmp)->next;

    }

    return 0;
}