summaryrefslogtreecommitdiff
path: root/src/offlinelogstorage/dlt_offline_logstorage.h
blob: e28e53592248bec6340ad2966c340eee6e9e6731 (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
/**
 * @licence app begin@
 * 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 header 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.h
 * For further information see http://www.genivi.org/.
 * @licence end@
 */

/*******************************************************************************
**                                                                            **
**  SRC-MODULE: dlt_offline_logstorage.h                                      **
**                                                                            **
**  TARGET    : linux                                                         **
**                                                                            **
**  PROJECT   : DLT                                                           **
**                                                                            **
**  AUTHOR    : Syed Hameed shameed@jp.adit-jv.com                            **
**              Christoph Lipka clipka@jp.adit-jv.com                         **
**  PURPOSE   :                                                               **
**                                                                            **
**  REMARKS   :                                                               **
**                                                                            **
**  PLATFORM DEPENDANT [yes/no]: yes                                          **
**                                                                            **
**  TO BE CHANGED BY USER [yes/no]: no                                        **
**                                                                            **
*******************************************************************************/

/*******************************************************************************
**                      Author Identity                                       **
********************************************************************************
**                                                                            **
** Initials     Name                       Company                            **
** --------     -------------------------  ---------------------------------- **
**  sh          Syed Hameed                ADIT                               **
**  cl          Christoph Lipka            ADIT                               **
*******************************************************************************/

#ifndef DLT_OFFLINE_LOGSTORAGE_H
#define DLT_OFFLINE_LOGSTORAGE_H

#include <search.h>
#include <stdbool.h>
#include "dlt_common.h"
#include "dlt-daemon_cfg.h"
#include "dlt_config_file_parser.h"

#define DLT_OFFLINE_LOGSTORAGE_MAXIDS               100 /* Maximum entries for each apids and ctids */
#define DLT_OFFLINE_LOGSTORAGE_MAX_POSSIBLE_KEYS   7  /* Max number of possible keys when searching for */

#define DLT_OFFLINE_LOGSTORAGE_INIT_DONE           1  /* For device configuration status */
#define DLT_OFFLINE_LOGSTORAGE_DEVICE_CONNECTED    1
#define DLT_OFFLINE_LOGSTORAGE_FREE                0
#define DLT_OFFLINE_LOGSTORAGE_DEVICE_DISCONNECTED 0
#define DLT_OFFLINE_LOGSTORAGE_CONFIG_DONE         1

#define DLT_OFFLINE_LOGSTORAGE_SYNC_CACHES         2  /* sync logstorage caches */

#define DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN         15  /* Maximum size for key */
#define DLT_OFFLINE_LOGSTORAGE_MAX_FILE_NAME_LEN   50  /* Maximum file name length of the log file */

#define DLT_OFFLINE_LOGSTORAGE_FILE_EXTENSION_LEN   4
#define DLT_OFFLINE_LOGSTORAGE_INDEX_LEN            3
#define DLT_OFFLINE_LOGSTORAGE_MAX_INDEX          999
#define DLT_OFFLINE_LOGSTORAGE_TIMESTAMP_LEN       16
#define DLT_OFFLINE_LOGSTORAGE_INDEX_OFFSET        (DLT_OFFLINE_LOGSTORAGE_TIMESTAMP_LEN + \
                                                    DLT_OFFLINE_LOGSTORAGE_FILE_EXTENSION_LEN + \
                                                    DLT_OFFLINE_LOGSTORAGE_INDEX_LEN)
#define DLT_OFFLINE_LOGSTORAGE_MAX_LOG_FILE_LEN    (DLT_OFFLINE_LOGSTORAGE_MAX_FILE_NAME_LEN + \
                                                    DLT_OFFLINE_LOGSTORAGE_TIMESTAMP_LEN + \
                                                    DLT_OFFLINE_LOGSTORAGE_INDEX_LEN + \
                                                    DLT_OFFLINE_LOGSTORAGE_FILE_EXTENSION_LEN + 1)

#define DLT_OFFLINE_LOGSTORAGE_CONFIG_DIR_PATH_LEN 50
#define DLT_OFFLINE_LOGSTORAGE_CONFIG_FILE_NAME    "dlt_logstorage.conf"

/* +3 because of device number and \0 */
#define DLT_OFFLINE_LOGSTORAGE_MAX_PATH_LEN (DLT_OFFLINE_LOGSTORAGE_MAX_LOG_FILE_LEN + \
                                             DLT_OFFLINE_LOGSTORAGE_CONFIG_DIR_PATH_LEN + 3)

#define DLT_OFFLINE_LOGSTORAGE_MAX(A, B)   ((A) > (B) ? (A) : (B))
#define DLT_OFFLINE_LOGSTORAGE_MIN(A, B)   ((A) < (B) ? (A) : (B))

#define DLT_OFFLINE_LOGSTORAGE_MAX_WRITE_ERRORS     5
#define DLT_OFFLINE_LOGSTORAGE_MAX_KEY_NUM          8

#define DLT_OFFLINE_LOGSTORAGE_CONFIG_SECTION "FILTER"
#define DLT_OFFLINE_LOGSTORAGE_GENERAL_CONFIG_SECTION "GENERAL"
#define DLT_OFFLINE_LOGSTORAGE_NONVERBOSE_STORAGE_SECTION "NON-VERBOSE-STORAGE-FILTER"
#define DLT_OFFLINE_LOGSTORAGE_NONVERBOSE_CONTROL_SECTION "NON-VERBOSE-LOGLEVEL-CTRL"

/* Offline Logstorage sync strategies */
#define DLT_LOGSTORAGE_SYNC_ON_ERROR                  -1 /* error case */
#define DLT_LOGSTORAGE_SYNC_UNSET                     0  /* strategy not set */
#define DLT_LOGSTORAGE_SYNC_ON_MSG                    1 /* default, on message sync */
#define DLT_LOGSTORAGE_SYNC_ON_DAEMON_EXIT            (1 << 1) /* sync on daemon exit */
#define DLT_LOGSTORAGE_SYNC_ON_DEMAND                 (1 << 2) /* sync on demand */
#define DLT_LOGSTORAGE_SYNC_ON_DEVICE_DISCONNECT      (1 << 3) /* sync on device disconnect*/
#define DLT_LOGSTORAGE_SYNC_ON_SPECIFIC_SIZE          (1 << 4) /* sync on after specific size */
#define DLT_LOGSTORAGE_SYNC_ON_FILE_SIZE              (1 << 5) /* sync on file size reached */

#define DLT_OFFLINE_LOGSTORAGE_IS_STRATEGY_SET(S, s) ((S)&(s))

/* logstorage max cache */
unsigned int g_logstorage_cache_max;
/* current logstorage cache size */
unsigned int g_logstorage_cache_size;

typedef struct
{
    unsigned int offset;          /* current write offset */
    unsigned int wrap_around_cnt; /* wrap around counter */
    unsigned int last_sync_offset; /* last sync position */
} DltLogStorageCacheFooter;

typedef struct
{
    /* File name user configurations */
    int logfile_timestamp;              /* Timestamp set/reset */
    char logfile_delimiter;             /* Choice of delimiter */
    unsigned int logfile_maxcounter;    /* Maximum file index counter */
    unsigned int logfile_counteridxlen; /* File index counter length */
} DltLogStorageUserConfig;

typedef struct DltLogStorageFileList
{
    /* List for filenames */
    char *name;                         /* Filename */
    unsigned int idx;                   /* File index */
    struct DltLogStorageFileList *next;
} DltLogStorageFileList;

typedef struct DltLogStorageFilterConfig DltLogStorageFilterConfig;

struct DltLogStorageFilterConfig
{
    /* filter section */
    char *apids;                    /* Application IDs configured for filter */
    char *ctids;                    /* Context IDs configured for filter */
    int log_level;                  /* Log level number configured for filter */
    int reset_log_level;            /* reset Log level to be sent on disconnect */
    char *file_name;                /* File name for log storage configured for filter */
    unsigned int file_size;         /* MAX File size of storage file configured for filter */
    unsigned int num_files;         /* MAX number of storage files configured for filters */
    int sync;                       /* Sync strategy */
    char *ecuid;                    /* ECU identifier */
    /* callback function for filter configurations */
    int (*dlt_logstorage_prepare)(DltLogStorageFilterConfig *config,
                                  DltLogStorageUserConfig *file_config,
                                  char *dev_path,
                                  int log_msg_size);
    int (*dlt_logstorage_write)(DltLogStorageFilterConfig *config,
                                unsigned char *data1,
                                int size1,
                                unsigned char *data2,
                                int size2,
                                unsigned char *data3,
                                int size3);
    /* status is strategy, e.g. DLT_LOGSTORAGE_SYNC_ON_MSG is used when callback
     * is called on message received */
    int (*dlt_logstorage_sync)(DltLogStorageFilterConfig *config,
                               DltLogStorageUserConfig *uconfig,
                               char *dev_path,
                               int status);
    FILE *log;                      /* current open log file */
    void *cache;                    /* log data cache */
    unsigned int specific_size;     /* cache size used for specific_size sync strategy */
    unsigned int current_write_file_offset;    /* file offset for specific_size sync strategy */
    unsigned int total_write_count; /* total count of data need to sync to file */
    bool pre_cache_sync;            /* sync done in previous wrap around */
    bool cur_cache_sync;            /* sync done in present cache */
    bool sync_from_start;           /* sync done from start of cache */
    DltLogStorageFileList *records; /* File name list */
};

typedef struct DltLogStorageFilterList DltLogStorageFilterList;

struct DltLogStorageFilterList
{
    char *key_list;                   /* List of key */
    int num_keys;                     /* Number of keys */
    DltLogStorageFilterConfig *data;  /* Filter data */
    DltLogStorageFilterList *next;    /* Pointer to next */
};

typedef struct
{
    DltLogStorageFilterList *config_list; /* List of all filters */
    DltLogStorageUserConfig uconfig;   /* User configurations for file name*/
    int num_configs;                   /* Number of configs */
    char device_mount_point[DLT_MOUNT_PATH_MAX + 1]; /* Device mount path */
    unsigned int connection_type;      /* Type of connection */
    unsigned int config_status;        /* Status of configuration */
    int write_errors;                  /* number of write errors */
} DltLogStorage;

typedef struct {
    char *key; /* The configuration key */
    int (*func)(DltLogStorage *handle, char *value); /* conf handler */
    int is_opt; /* If configuration is optional or not */
} DltLogstorageGeneralConf;

typedef enum {
    DLT_LOGSTORAGE_GENERAL_CONF_BLOCKMODE = 0,
    DLT_LOGSTORAGE_GENERAL_CONF_COUNT
} DltLogstorageGeneralConfType;

typedef struct {
    char *key; /* Configuration key */
    int (*func)(DltLogStorageFilterConfig *config, char *value); /* conf handler */
    int is_opt; /* If configuration is optional or not */
} DltLogstorageFilterConf;

typedef enum {
    DLT_LOGSTORAGE_FILTER_CONF_LOGAPPNAME = 0,
    DLT_LOGSTORAGE_FILTER_CONF_CONTEXTNAME,
    DLT_LOGSTORAGE_FILTER_CONF_LOGLEVEL,
    DLT_LOGSTORAGE_FILTER_CONF_RESET_LOGLEVEL,
    DLT_LOGSTORAGE_FILTER_CONF_FILE,
    DLT_LOGSTORAGE_FILTER_CONF_FILESIZE,
    DLT_LOGSTORAGE_FILTER_CONF_NOFILES,
    DLT_LOGSTORAGE_FILTER_CONF_SYNCBEHAVIOR,
    DLT_LOGSTORAGE_FILTER_CONF_ECUID,
    DLT_LOGSTORAGE_FILTER_CONF_SPECIFIC_SIZE,
    DLT_LOGSTORAGE_FILTER_CONF_COUNT
} DltLogstorageFilterConfType;

/**
 * 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
 */
int dlt_logstorage_device_connected(DltLogStorage *handle,
                                    char *mount_point);

/**
 * dlt_logstorage_device_disconnected
 * De-Initializes DLT Offline Logstorage with respect to device status
 *
 * @param handle         DLT Logstorage handle
 * @param reason         Reason for device disconnection
 * @return               0 on success, -1 on error
 */
int dlt_logstorage_device_disconnected(DltLogStorage *handle,
                                       int reason);
/**
 * dlt_logstorage_get_config
 *
 * Obtain the configuration data of all filters for provided apid and ctid
 * For a given apid and ctid, there can be 3 possiblities of configuration
 * data available in the Hash map, this function will return the address
 * of configuration data for all these 3 combinations
 *
 * @param handle    DltLogStorage handle
 * @param config    Pointer to array of filter configurations
 * @param appid     application id
 * @param ctxid     context id
 * @return          number of found configurations
 */
int dlt_logstorage_get_config(DltLogStorage *handle,
                              DltLogStorageFilterConfig **config,
                              char *apid,
                              char *ctid,
                              char *ecuid);

/**
 * 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 available 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);

/**
 * 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 message body
 * @param size2     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);

/**
 * dlt_logstorage_sync_caches
 *
 * Sync all caches inside the specified logstorage device.
 *
 * @param  handle    DltLogStorage handle
 * @return 0 on success, -1 otherwise
 */
int dlt_logstorage_sync_caches(DltLogStorage *handle);

#endif /* DLT_OFFLINE_LOGSTORAGE_H */