summaryrefslogtreecommitdiff
path: root/src/lib/dlt_filetransfer.c
blob: a9b30bf9a5c4a22e8a853e318d6acd5af91c5bfe (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
#include "dlt_filetransfer.h"

//!Defines the buffer size of a single file package which will be logged to dlt
#define BUFFER_SIZE 1024

//!Defines the minimum timeout between two dlt logs. This is important because dlt should not be flooded with too many logs in a short period of time.
#define MIN_TIMEOUT 20


#define DLT_FILETRANSFER_TRANSFER_ALL_PACKAGES LONG_MAX


//!Buffer for dlt file transfer. The size is defined by BUFFER_SIZE
unsigned char buffer[BUFFER_SIZE];


//!Get some information about the file size of a file
/**See stat(2) for more informations.
 * @param file Absolute file path
 * @return Returns the size of the file (if it is a regular file or a symbolic link) in bytes.
 */
unsigned long getFilesize(const char* file){
	struct stat st;
	stat(file, &st);
	return (unsigned long)st.st_size;
}

//!Get some information about the file serial number of a file
/** See stat(2) for more informations.
 * @param file Absolute file path
 * @return Returns a unique number associated with each filename
 */
unsigned long getFileSerialNumber(const char* file){
	struct stat st;
	stat(file, &st);
	return (unsigned long)st.st_ino;
}

//!Returns the creation date of a file
/** See stat(2) for more informations.
 * @param file Absolute file path
 * @return Returns the creation date of a file
*/
time_t getFileCreationDate(const char* file){
	struct stat st;
	stat(file, &st);
    	return st.st_ctime;
}

//!Returns the creation date of a file
/** Format of the creation date is Day Mon dd hh:mm:ss yyyy
 * @param file Absolute file path
 * @return Returns the creation date of a file
*/
char* getFileCreationDate2(const char* file){
	struct stat st;
	stat(file, &st);
	
	struct tm  *ts= localtime(&st.st_ctime);
	return asctime(ts);
}

//!Checks if the file exists
/**@param file Absolute file path
 * @return Returns 1 if the file exists, 0 if the file does not exist
 */
int isFile (const char* file)
{
	struct stat   st;   
	return (stat (file, &st) == 0);
}

//!Waits a period of time
/**Waits a period of time. The minimal time to wait is MIN_TIMEOUT. This makes sure that the FIFO of dlt is not flooded.
 * @param timeout Timeout to in ms but can not be smaller as MIN_TIMEOUT
 */
void doTimeout(int timeout)
{
	usleep(timeout * 1000);
}

//!Checks free space of the user buffer
/**
 * @param returns -1 if more than 50% space in the user buffer is free. Otherwise 1 will be returned.
 */
int checkUserBufferForFreeSpace()
{
	int total_size, used_size;

	dlt_user_check_buffer(&total_size, &used_size);

	if((total_size - used_size) < (total_size/2))
	{
		return -1;
	}
	return 1;
}

//!Deletes the given file
/**
 * @param filename Absolute file path
 * @return If the file is successfully deleted, a zero value is returned.If the file can not be deleted a nonzero value is returned.
 */
int doRemoveFile(const char*filename){
	return remove( filename); 
}

void dlt_user_log_file_errorMessage(DltContext *fileContext, const char *filename, int errorCode){

	if(errno != ENOENT)
	{
		DLT_LOG(*fileContext,DLT_LOG_ERROR,
			DLT_STRING("FLER"),
			DLT_INT(errorCode),
			DLT_INT(-errno),
			DLT_UINT(getFileSerialNumber(filename)),
			DLT_STRING(filename),
			DLT_UINT(getFilesize(filename)),
			DLT_STRING(getFileCreationDate2(filename)),
			DLT_UINT(dlt_user_log_file_packagesCount(fileContext,filename)),
			DLT_UINT(BUFFER_SIZE),
			DLT_STRING("FLER")
		);		
	} else {
		DLT_LOG(*fileContext,DLT_LOG_ERROR,
			DLT_STRING("FLER"),
			DLT_INT(errorCode),
			DLT_INT(-errno),
			DLT_STRING(filename),
			DLT_STRING("FLER")
		);
	}
}



//!Logs specific file inforamtions to dlt
/**The filename, file size, file serial number and the number of packages will be logged to dlt.
 * @param fileContext Specific context
 * @param filename Absolute file path
 * @return Returns 0 if everything was okey.If there was a failure a value < 0 will be returned.
 */
int dlt_user_log_file_infoAbout(DltContext *fileContext, const char *filename){
	
	if(isFile(filename))
	{
		DLT_LOG(*fileContext,DLT_LOG_INFO,
			DLT_STRING("FLIF"),
			DLT_STRING("file serialnumber"),DLT_UINT(getFileSerialNumber(filename)),
			DLT_STRING("filename"),DLT_STRING(filename),
			DLT_STRING("file size in bytes"),DLT_UINT(getFilesize(filename)),
			DLT_STRING("file creation date"),DLT_STRING(getFileCreationDate2(filename)),
			DLT_STRING("number of packages"),DLT_UINT(dlt_user_log_file_packagesCount(fileContext, filename)),
			DLT_STRING("FLIF")
		);
		return 0;
	} else {
		dlt_user_log_file_errorMessage(fileContext,filename,DLT_FILETRANSFER_ERROR_INFO_ABOUT);
		return DLT_FILETRANSFER_ERROR_INFO_ABOUT;
	}
}

//!Transfer the complete file as several dlt logs.
/**This method transfer the complete file as several dlt logs. At first it will be checked that the file exist.
 * In the next step some generic informations about the file will be logged to dlt.
 * Now the header will be logged to dlt. See the method dlt_user_log_file_header for more informations.
 * Then the method dlt_user_log_data will be called with the parameter to log all packages in a loop with some timeout.
 * At last dlt_user_log_end is called to signal that the complete file transfer was okey. This is important for the plugin of the dlt viewer. 
 * @param fileContext Specific context to log the file to dlt
 * @param filename Absolute file path
 * @param deleteFlag Flag if the file will be deleted after transfer. 1->delete, 0->notDelete
 * @param timeout Timeout in ms to wait between some logs. Important that the FIFO of dlt will not be flooded with to many messages in a short period of time.
 * @return Returns 0 if everything was okey. If there was a failure a value < 0 will be returned.
 */
int dlt_user_log_file_complete(DltContext *fileContext, const char *filename, int deleteFlag, int timeout)
{	
	if(!isFile(filename))
	{
		dlt_user_log_file_errorMessage(fileContext,filename, DLT_FILETRANSFER_ERROR_FILE_COMPLETE);
		return DLT_FILETRANSFER_ERROR_FILE_COMPLETE;
	}
	
	if(dlt_user_log_file_header(fileContext,filename) != 0)
	{
		return DLT_FILETRANSFER_ERROR_FILE_COMPLETE1;
	}
		
	if(dlt_user_log_file_data(fileContext, filename,DLT_FILETRANSFER_TRANSFER_ALL_PACKAGES,timeout) != 0)
	{
		return DLT_FILETRANSFER_ERROR_FILE_COMPLETE2;
	}
		
	if(dlt_user_log_file_end(fileContext,filename, deleteFlag) != 0)
	{
		return DLT_FILETRANSFER_ERROR_FILE_COMPLETE3;
	}		
			
	return 0;
}

//!This method gives information about the number of packages the file have
/**Every file will be divided into several packages. Every package will be logged as a single dlt log.
 * The number of packages depends on the BUFFER_SIZE.
 * At first it will be checked if the file exist. Then the file will be divided into
 * several packages depending on the buffer size.
 * @param fileContext Specific context to log the file to dlt
 * @param filename Absolute file path
 * @return Returns the number of packages if everything was okey. If there was a failure a value < 0 will be returned.
 */
int dlt_user_log_file_packagesCount(DltContext *fileContext, const char *filename){
	int packages;
	long filesize;
	
	if(isFile(filename))
	{
		packages = 1;
		filesize = getFilesize(filename);
		if(filesize < BUFFER_SIZE)
		{	
			return packages;
		} 
		else 
		{
			packages = filesize/BUFFER_SIZE;
			
			if(filesize%BUFFER_SIZE == 0)
			{	
				return packages;
			}
			else
			{
				return packages+1;
			}
		}
	} else {
		dlt_user_log_file_errorMessage(fileContext,filename,DLT_FILETRANSFER_ERROR_PACKAGE_COUNT);
		return -1;
	}
}

//!Transfer the head of the file as a dlt logs.
/**The head of the file must be logged to dlt because the head contains inforamtion about the file serial number,
 * the file name, the file size, package number the file have and the buffer size.
 * All these informations are needed from the plugin of the dlt viewer.
 * See the Mainpages.c for more informations.
 * @param fileContext Specific context to log the file to dlt
 * @param filename Absolute file path
 * @return Returns 0 if everything was okey. If there was a failure a value < 0 will be returned.
 */
int dlt_user_log_file_header(DltContext *fileContext,const char *filename){

	if(isFile(filename))
	{
		DLT_LOG(*fileContext,DLT_LOG_INFO,
					DLT_STRING("FLST"),
					DLT_UINT(getFileSerialNumber(filename)),
					DLT_STRING(filename),
					DLT_UINT(getFilesize(filename)),
					DLT_STRING(getFileCreationDate2(filename));
					DLT_UINT(dlt_user_log_file_packagesCount(fileContext,filename)),
					DLT_UINT(BUFFER_SIZE),
					DLT_STRING("FLST")		
				);

		return 0;
	}
	else
	{
		dlt_user_log_file_errorMessage(fileContext,filename, DLT_FILETRANSFER_ERROR_FILE_HEAD);
		return DLT_FILETRANSFER_ERROR_FILE_HEAD;
	}
}

//!Transfer the content data of a file.
/**See the Mainpages.c for more informations.
 * @param fileContext Specific context to log the file to dlt
 * @param filename Absolute file path
 * @param packageToTransfer Package number to transfer. If this param is LONG_MAX, the whole file will be transferred with a specific timeout
 * @param timeout Timeout to wait between dlt logs. Important because the dlt FIFO should not be flooded. Default is defined by MIN_TIMEOUT. The given timeout in ms can not be smaller than MIN_TIMEOUT.
 * @return Returns 0 if everything was okey. If there was a failure a value < 0 will be returned.
 */
int dlt_user_log_file_data(DltContext *fileContext,const char *filename, int packageToTransfer, int timeout){
	FILE *file;
	int pkgNumber;
	long readBytes;
		
	if(isFile(filename))
	{
	
		file = fopen (filename,"rb");
		if (file == NULL)
		{
			dlt_user_log_file_errorMessage(fileContext,filename,DLT_FILETRANSFER_ERROR_FILE_DATA);
			return DLT_FILETRANSFER_ERROR_FILE_DATA;
		}
		
		if( (packageToTransfer != DLT_FILETRANSFER_TRANSFER_ALL_PACKAGES && packageToTransfer > dlt_user_log_file_packagesCount(fileContext,filename)) || packageToTransfer <= 0)
		{
			DLT_LOG(*fileContext,DLT_LOG_ERROR,
				DLT_STRING("Error at dlt_user_log_file_data: packageToTransfer out of scope"),
				DLT_STRING("packageToTransfer:"),
				DLT_UINT(packageToTransfer),
				DLT_STRING("numberOfMaximalPackages:"),
				DLT_UINT(dlt_user_log_file_packagesCount(fileContext,filename)),
				DLT_STRING("for File:"),
				DLT_STRING(filename)
			);
			return DLT_FILETRANSFER_ERROR_FILE_DATA;
		}

		readBytes = 0;
		
		if(packageToTransfer != DLT_FILETRANSFER_TRANSFER_ALL_PACKAGES)
		{
//				If a single package should be transferred. The user has to check that the free space in the user buffer > 50%
//				if(checkUserBufferForFreeSpace()<0)
//					return DLT_FILETRANSFER_ERROR_FILE_DATA_USER_BUFFER_FAILED;

				fseek ( file , (packageToTransfer-1)*BUFFER_SIZE , SEEK_SET );
				readBytes = fread(buffer, sizeof(char), BUFFER_SIZE, file);

				DLT_LOG(*fileContext,DLT_LOG_INFO,
				DLT_STRING("FLDA"),
				DLT_UINT(getFileSerialNumber(filename)),
				DLT_UINT(packageToTransfer),
				DLT_RAW(buffer,readBytes),
				DLT_STRING("FLDA")
				);

				doTimeout(timeout);

		} else {
			pkgNumber = 0;
			while( !feof( file ) )
			{
//				If the complete file should be transferred, the user buffer will be checked.
//				If free space < 50% the package won't be transferred.
				if(checkUserBufferForFreeSpace()>0)
				{
					pkgNumber++;
					readBytes = fread(buffer, sizeof(char), BUFFER_SIZE, file);
				
					DLT_LOG(*fileContext,DLT_LOG_INFO,
							DLT_STRING("FLDA"),
							DLT_UINT(getFileSerialNumber(filename)),
							DLT_UINT(pkgNumber),
							DLT_RAW(buffer,readBytes),
							DLT_STRING("FLDA")
					);
				}
				doTimeout(timeout);
			}
		}
		
		fclose(file);
		
		return 0;
		
	} else {
		dlt_user_log_file_errorMessage(fileContext,filename,DLT_FILETRANSFER_ERROR_FILE_DATA);
		return DLT_FILETRANSFER_ERROR_FILE_DATA;
	}
	
}
//!Transfer the end of the file as a dlt logs.
/**The end of the file must be logged to dlt because the end contains inforamtion about the file serial number.
 * This informations is needed from the plugin of the dlt viewer.
 * See the Mainpages.c for more informations.
 * @param fileContext Specific context to log the file to dlt
 * @param filename Absolute file path
 * @param deleteFlag Flag to delete the file after the whole file is transferred (logged to dlt).1->delete,0->NotDelete
 * @return Returns 0 if everything was okey. If there was a failure a value < 0 will be returned.
 */
int dlt_user_log_file_end(DltContext *fileContext,const char *filename,int deleteFlag){

	if(isFile(filename))
	{

		DLT_LOG(*fileContext,DLT_LOG_INFO,
				DLT_STRING("FLFI"),
				DLT_UINT(getFileSerialNumber(filename)),
				DLT_STRING("FLFI")
		);
		
		if(deleteFlag){
				if( doRemoveFile(filename) != 0 ){
					dlt_user_log_file_errorMessage(fileContext,filename,DLT_FILETRANSFER_ERROR_FILE_END);
					return -1;
				}
		}
	
		return 0;
	}else{
		dlt_user_log_file_errorMessage(fileContext,filename,DLT_FILETRANSFER_ERROR_FILE_END);
		return DLT_FILETRANSFER_ERROR_FILE_END;
	}
}