automotive-dlt
dlt_filetransfer.c
Go to the documentation of this file.
1 /*
2  * @licence app begin@
3  * SPDX license identifier: MPL-2.0
4  *
5  * Copyright (C) 2011-2015, BMW AG
6  *
7  * This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
8  *
9  * This Source Code Form is subject to the terms of the
10  * Mozilla Public License (MPL), v. 2.0.
11  * If a copy of the MPL was not distributed with this file,
12  * You can obtain one at http://mozilla.org/MPL/2.0/.
13  *
14  * For further information see http://www.genivi.org/.
15  * @licence end@
16  */
17 
27 /*******************************************************************************
28 ** **
29 ** SRC-MODULE: dlt-test-client.c **
30 ** **
31 ** TARGET : linux **
32 ** **
33 ** PROJECT : DLT **
34 ** **
35 ** AUTHOR : Alexander Wenzel Alexander.AW.Wenzel@bmw.de **
36 ** **
37 ** PURPOSE : **
38 ** **
39 ** REMARKS : **
40 ** **
41 ** PLATFORM DEPENDANT [yes/no]: yes **
42 ** **
43 ** TO BE CHANGED BY USER [yes/no]: no **
44 ** **
45 *******************************************************************************/
46 
47 /*******************************************************************************
48 ** Author Identity **
49 ********************************************************************************
50 ** **
51 ** Initials Name Company **
52 ** -------- ------------------------- ---------------------------------- **
53 ** aw Alexander Wenzel BMW **
54 *******************************************************************************/
55 
56 #include <errno.h>
57 #include <stdio.h>
58 #include <string.h>
59 #include "dlt_filetransfer.h"
60 #include "dlt_common.h"
61 
63 #define BUFFER_SIZE 1024
64 
66 #define MIN_TIMEOUT 20
67 
68 
69 #define DLT_FILETRANSFER_TRANSFER_ALL_PACKAGES INT_MAX
70 
71 
73 unsigned char buffer[BUFFER_SIZE];
74 
75 
77 
81 uint32_t getFilesize(const char* file, int *ok){
82  struct stat st;
83 
84  if ( -1 == stat(file, &st))
85  {
86  //we can only return 0, as the value is unsigned
87  *ok = 0;
88  return 0;
89  }
90  *ok = 1;
91  return (uint32_t)st.st_size;
92 }
93 
99 void stringHash(const char* str, uint32_t *hash )
100 {
101  if (!str || !hash)
102  return;
103  unsigned int len = strlen(str);
104 
105  unsigned int i = 0;
106  if (len <= 0){
107  return;
108  }
109 
110  for(i = 0; i < len; i++)
111  {
112  *hash = 53 * *hash + str[i];
113  }
114 
115 }
116 
117 
119 
124 uint32_t getFileSerialNumber(const char* file, int *ok){
125  struct stat st;
126  uint32_t ret;
127  if ( -1 == stat(file, &st))
128  {
129  *ok = 0;
130  ret = 0;
131  }
132  else
133  {
134  *ok = 1;
135  ret = st.st_ino;
136  ret = ret << (sizeof(ret)*8)/2;
137  ret |= st.st_size;
138  ret ^= st.st_ctime;
139  stringHash(file, &ret);
140  }
141  return ret;
142 }
143 
145 
149 time_t getFileCreationDate(const char* file,int *ok){
150  struct stat st;
151  if (-1 == stat(file, &st))
152  {
153  *ok = 0;
154  return 0;
155  }
156  *ok = 1;
157  return st.st_ctime;
158 }
159 
161 
165 char* getFileCreationDate2(const char* file,int *ok){
166  struct stat st;
167  if (-1 == stat(file, &st))
168  {
169  *ok = 0;
170  return 0;
171  }
172  *ok = 1;
173  struct tm *ts= localtime(&st.st_ctime);
174  return asctime(ts);
175 }
176 
178 
181 int isFile (const char* file)
182 {
183  struct stat st;
184  return (stat (file, &st) == 0);
185 }
186 
188 
191 void doTimeout(int timeout)
192 {
193  usleep(timeout * 1000);
194 }
195 
197 
201 {
202  int total_size, used_size;
203 
204  dlt_user_check_buffer(&total_size, &used_size);
205 
206  if((total_size - used_size) < (total_size/2))
207  {
208  return -1;
209  }
210  return 1;
211 }
212 
214 
218 int doRemoveFile(const char*filename){
219  return remove( filename);
220 }
221 
222 void dlt_user_log_file_errorMessage(DltContext *fileContext, const char *filename, int errorCode){
223 
224  if(errno != ENOENT)
225  {
226  int ok = 0;
227  uint32_t fserial = getFileSerialNumber(filename,&ok);
228  if (!ok)
229  DLT_LOG(*fileContext,DLT_LOG_ERROR,DLT_STRING("dlt_user_log_file_errorMessage, error in getFileSerialNumber for: "),DLT_STRING(filename));
230  uint32_t fsize = getFilesize(filename,&ok);
231  if (!ok)
232  DLT_LOG(*fileContext,DLT_LOG_ERROR,DLT_STRING("dlt_user_log_file_errorMessage, error in getFilesize for: "),DLT_STRING(filename));
233  char *fcreationdate = getFileCreationDate2(filename,&ok);
234  if (!ok)
235  DLT_LOG(*fileContext,DLT_LOG_ERROR,DLT_STRING("dlt_user_log_file_errorMessage, error in getFilesize for: "),DLT_STRING(filename));
236 
237  int package_count = dlt_user_log_file_packagesCount(fileContext,filename);
238 
239  DLT_LOG(*fileContext,DLT_LOG_ERROR,
240  DLT_STRING("FLER"),
241  DLT_INT(errorCode),
242  DLT_INT(-errno),
243  DLT_UINT(fserial),
244  DLT_STRING(filename),
245  DLT_UINT(fsize),
246  DLT_STRING(fcreationdate),
247  DLT_INT(package_count),
249  DLT_STRING("FLER")
250  );
251  } else {
252  DLT_LOG(*fileContext,DLT_LOG_ERROR,
253  DLT_STRING("FLER"),
254  DLT_INT(errorCode),
255  DLT_INT(-errno),
256  DLT_STRING(filename),
257  DLT_STRING("FLER")
258  );
259  }
260 }
261 
262 
263 
265 
270 int dlt_user_log_file_infoAbout(DltContext *fileContext, const char *filename){
271 
272  if(isFile(filename))
273  {
274  int ok;
275 
276  uint32_t fsize = getFilesize(filename,&ok);
277  if (!ok)
278  DLT_LOG(*fileContext,DLT_LOG_ERROR,DLT_STRING("dlt_user_log_file_infoAbout, Error getting size of file:"),DLT_STRING(filename));
279 
280  uint32_t fserialnumber = getFileSerialNumber(filename,&ok);
281  if (!ok)
282  DLT_LOG(*fileContext,DLT_LOG_ERROR,DLT_STRING("dlt_user_log_file_infoAbout, Error getting serial number of file:"),DLT_STRING(filename));
283 
284 
285  char *creationdate = getFileCreationDate2(filename,&ok);
286  if (!ok)
287  DLT_LOG(*fileContext,DLT_LOG_ERROR,DLT_STRING("dlt_user_log_file_infoAbout, Error getting creation date of file:"),DLT_STRING(filename));
288 
289  DLT_LOG(*fileContext,DLT_LOG_INFO,
290  DLT_STRING("FLIF"),
291  DLT_STRING("file serialnumber"),DLT_UINT(fserialnumber),
292  DLT_STRING("filename"),DLT_STRING(filename),
293  DLT_STRING("file size in bytes"),DLT_UINT(fsize),
294  DLT_STRING("file creation date"),DLT_STRING(creationdate),
295  DLT_STRING("number of packages"),DLT_UINT(dlt_user_log_file_packagesCount(fileContext, filename)),
296  DLT_STRING("FLIF")
297  );
298  return 0;
299  } else {
302  }
303 }
304 
306 
317 int dlt_user_log_file_complete(DltContext *fileContext, const char *filename, int deleteFlag, int timeout)
318 {
319  if(!isFile(filename))
320  {
323  }
324 
325  if(dlt_user_log_file_header(fileContext,filename) != 0)
326  {
328  }
329 
330  if(dlt_user_log_file_data(fileContext, filename,DLT_FILETRANSFER_TRANSFER_ALL_PACKAGES,timeout) != 0)
331  {
333  }
334 
335  if(dlt_user_log_file_end(fileContext,filename, deleteFlag) != 0)
336  {
338  }
339 
340  return 0;
341 }
342 
344 
352 int dlt_user_log_file_packagesCount(DltContext *fileContext, const char *filename){
353  int packages;
354  uint32_t filesize;
355 
356  if(isFile(filename))
357  {
358  packages = 1;
359  int ok;
360  filesize = getFilesize(filename,&ok);
361  if (!ok){
362  DLT_LOG(*fileContext,DLT_LOG_ERROR,DLT_STRING("Error in: dlt_user_log_file_packagesCount, isFile"),DLT_STRING(filename),DLT_INT(DLT_FILETRANSFER_ERROR_PACKAGE_COUNT));
363  return -1;
364  }
365  if(filesize < BUFFER_SIZE)
366  {
367  return packages;
368  }
369  else
370  {
371  packages = filesize/BUFFER_SIZE;
372 
373  if(filesize%BUFFER_SIZE == 0)
374  {
375  return packages;
376  }
377  else
378  {
379  return packages+1;
380  }
381  }
382  } else {
383  DLT_LOG(*fileContext,DLT_LOG_ERROR,DLT_STRING("Error in: dlt_user_log_file_packagesCount, !isFile"),DLT_STRING(filename),DLT_INT(DLT_FILETRANSFER_ERROR_PACKAGE_COUNT));
384  return -1;
385  }
386 }
387 
389 
398 int dlt_user_log_file_header_alias(DltContext *fileContext,const char *filename, const char *alias){
399 
400  if(isFile(filename))
401  {
402  int ok;
403 
404  uint32_t fserialnumber = getFileSerialNumber(filename,&ok);
405  if (!ok)
406  DLT_LOG(*fileContext,DLT_LOG_ERROR,DLT_STRING("dlt_user_log_file_header_alias, Error getting serial number of file:"),DLT_STRING(filename));
407 
408  uint32_t fsize = getFilesize(filename,&ok);
409  if (!ok)
410  DLT_LOG(*fileContext,DLT_LOG_ERROR,DLT_STRING("dlt_user_log_file_header_alias, Error getting size of file:"),DLT_STRING(filename));
411 
412  char *fcreationdate = getFileCreationDate2(filename,&ok);
413  if (!ok)
414  DLT_LOG(*fileContext,DLT_LOG_ERROR,DLT_STRING("dlt_user_log_file_header_alias, Error getting creation date of file:"),DLT_STRING(filename));
415 
416 
417 
418  DLT_LOG(*fileContext,DLT_LOG_INFO,
419  DLT_STRING("FLST"),
420  DLT_UINT(fserialnumber),
421  DLT_STRING(alias),
422  DLT_UINT(fsize),
423  DLT_STRING(fcreationdate);
424  DLT_UINT(dlt_user_log_file_packagesCount(fileContext,filename)),
426  DLT_STRING("FLST")
427  );
428 
429  return 0;
430  }
431  else
432  {
435  }
436 }
437 
439 
447 int dlt_user_log_file_header(DltContext *fileContext,const char *filename){
448 
449  if(isFile(filename))
450  {
451  int ok;
452 
453  uint32_t fserialnumber = getFileSerialNumber(filename,&ok);
454  if (!ok)
455  DLT_LOG(*fileContext,DLT_LOG_ERROR,DLT_STRING("dlt_user_log_file_header, Error getting serial number of file:"),DLT_STRING(filename));
456 
457  uint32_t fsize = getFilesize(filename,&ok);
458  if (!ok)
459  DLT_LOG(*fileContext,DLT_LOG_ERROR,DLT_STRING("dlt_user_log_file_header, Error getting size of file:"),DLT_STRING(filename));
460 
461  char *fcreationdate = getFileCreationDate2(filename,&ok);
462  if (!ok)
463  DLT_LOG(*fileContext,DLT_LOG_ERROR,DLT_STRING("dlt_user_log_file_header, Error getting creation date of file:"),DLT_STRING(filename));
464 
465 
466 
467 
468  DLT_LOG(*fileContext,DLT_LOG_INFO,
469  DLT_STRING("FLST"),
470  DLT_UINT(fserialnumber),
471  DLT_STRING(filename),
472  DLT_UINT(fsize),
473  DLT_STRING(fcreationdate);
474  DLT_UINT(dlt_user_log_file_packagesCount(fileContext,filename)),
476  DLT_STRING("FLST")
477  );
478 
479  return 0;
480  }
481  else
482  {
485  }
486 }
487 
489 
496 int dlt_user_log_file_data(DltContext *fileContext,const char *filename, int packageToTransfer, int timeout){
497  FILE *file;
498  int pkgNumber;
499  uint32_t readBytes;
500 
501  if(isFile(filename))
502  {
503 
504  file = fopen (filename,"rb");
505  if (file == NULL)
506  {
509  }
510 
511  if( (packageToTransfer != DLT_FILETRANSFER_TRANSFER_ALL_PACKAGES && packageToTransfer > dlt_user_log_file_packagesCount(fileContext,filename)) || packageToTransfer <= 0)
512  {
513  DLT_LOG(*fileContext,DLT_LOG_ERROR,
514  DLT_STRING("Error at dlt_user_log_file_data: packageToTransfer out of scope"),
515  DLT_STRING("packageToTransfer:"),
516  DLT_UINT(packageToTransfer),
517  DLT_STRING("numberOfMaximalPackages:"),
518  DLT_UINT(dlt_user_log_file_packagesCount(fileContext,filename)),
519  DLT_STRING("for File:"),
520  DLT_STRING(filename)
521  );
522  fclose(file);
524  }
525 
526  readBytes = 0;
527 
528  if(packageToTransfer != DLT_FILETRANSFER_TRANSFER_ALL_PACKAGES)
529  {
530 // If a single package should be transferred. The user has to check that the free space in the user buffer > 50%
531 // if(checkUserBufferForFreeSpace()<0)
532 // return DLT_FILETRANSFER_ERROR_FILE_DATA_USER_BUFFER_FAILED;
533 
534  if ( 0 != fseek ( file , (packageToTransfer-1)*BUFFER_SIZE , SEEK_SET ) )
535  {
536  DLT_LOG(*fileContext,DLT_LOG_ERROR,
537  DLT_STRING("failed to fseek in file: "),
538  DLT_STRING(filename),
539  DLT_STRING("ferror:"),
540  DLT_INT(ferror(file))
541  );
542 
543  fclose (file);
544  return -1;
545 
546  }
547  readBytes = fread(buffer, sizeof(char), BUFFER_SIZE, file);
548  int ok;
549 
550  uint32_t fserial = getFileSerialNumber(filename,&ok);
551 
552  if (1 != ok)
553  {
554  DLT_LOG(*fileContext,DLT_LOG_ERROR,
555  DLT_STRING("failed to get FileSerialNumber for: "),
556  DLT_STRING(filename));
557  }
558 
559  DLT_LOG(*fileContext,DLT_LOG_INFO,
560  DLT_STRING("FLDA"),
561  DLT_UINT(fserial),
562  DLT_UINT(packageToTransfer),
563  DLT_RAW(buffer,readBytes),
564  DLT_STRING("FLDA")
565  );
566 
567  doTimeout(timeout);
568 
569  } else {
570  pkgNumber = 0;
571  while( !feof( file ) )
572  {
573 // If the complete file should be transferred, the user buffer will be checked.
574 // If free space < 50% the package won't be transferred.
576  {
577  pkgNumber++;
578  readBytes = fread(buffer, sizeof(char), BUFFER_SIZE, file);
579  int ok;
580 
581  uint32_t fserial = getFileSerialNumber(filename,&ok);
582 
583  if (1 != ok)
584  {
585  DLT_LOG(*fileContext,DLT_LOG_ERROR,
586  DLT_STRING("failed to get FileSerialNumber for: "),
587  DLT_STRING(filename));
588  }
589 
590  DLT_LOG(*fileContext,DLT_LOG_INFO,
591  DLT_STRING("FLDA"),
592  DLT_UINT(fserial),
593  DLT_UINT(pkgNumber),
594  DLT_RAW(buffer,readBytes),
595  DLT_STRING("FLDA")
596  );
597  }
598  doTimeout(timeout);
599  }
600  }
601 
602  fclose(file);
603 
604  return 0;
605 
606  } else {
609  }
610 
611 }
613 
621 int dlt_user_log_file_end(DltContext *fileContext,const char *filename,int deleteFlag){
622 
623  if(isFile(filename))
624  {
625 
626  int ok;
627  uint32_t fserial = getFileSerialNumber(filename,&ok);
628 
629  if (1 != ok)
630  {
631  DLT_LOG(*fileContext,DLT_LOG_ERROR,
632  DLT_STRING("failed to get FileSerialNumber for: "),
633  DLT_STRING(filename));
634  }
635 
636  DLT_LOG(*fileContext,DLT_LOG_INFO,
637  DLT_STRING("FLFI"),
638  DLT_UINT(fserial),
639  DLT_STRING("FLFI")
640  );
641 
642  if(deleteFlag){
643  if( doRemoveFile(filename) != 0 ){
645  return -1;
646  }
647  }
648 
649  return 0;
650  }else{
653  }
654 }
int checkUserBufferForFreeSpace()
Checks free space of the user buffer.
#define DLT_RAW(BUF, LEN)
int dlt_user_log_file_complete(DltContext *fileContext, const char *filename, int deleteFlag, int timeout)
Transfer the complete file as several dlt logs.
#define DLT_FILETRANSFER_ERROR_FILE_DATA
int doRemoveFile(const char *filename)
Deletes the given file.
#define DLT_INT(INT_VAR)
int dlt_user_log_file_packagesCount(DltContext *fileContext, const char *filename)
This method gives information about the number of packages the file have.
uint32_t getFileSerialNumber(const char *file, int *ok)
Get some information about the file serial number of a file.
int dlt_user_log_file_header_alias(DltContext *fileContext, const char *filename, const char *alias)
Transfer the head of the file as a dlt logs.
void stringHash(const char *str, uint32_t *hash)
char * getFileCreationDate2(const char *file, int *ok)
Returns the creation date of a file.
int isFile(const char *file)
Checks if the file exists.
DltReturnValue dlt_user_check_buffer(int *total_size, int *used_size)
Definition: dlt_user.c:4512
#define DLT_STRING(TEXT)
unsigned char buffer[BUFFER_SIZE]
Buffer for dlt file transfer. The size is defined by BUFFER_SIZE.
time_t getFileCreationDate(const char *file, int *ok)
Returns the creation date of a file.
#define DLT_FILETRANSFER_ERROR_FILE_COMPLETE
void doTimeout(int timeout)
Waits a period of time.
int dlt_user_log_file_header(DltContext *fileContext, const char *filename)
Transfer the head of the file as a dlt logs.
#define DLT_FILETRANSFER_ERROR_FILE_COMPLETE2
#define DLT_FILETRANSFER_ERROR_FILE_COMPLETE3
#define DLT_FILETRANSFER_ERROR_PACKAGE_COUNT
int dlt_user_log_file_end(DltContext *fileContext, const char *filename, int deleteFlag)
Transfer the end of the file as a dlt logs.
uint32_t getFilesize(const char *file, int *ok)
Get some information about the file size of a file.
int dlt_user_log_file_data(DltContext *fileContext, const char *filename, int packageToTransfer, int timeout)
Transfer the content data of a file.
int dlt_user_log_file_infoAbout(DltContext *fileContext, const char *filename)
Logs specific file inforamtions to dlt.
#define DLT_LOG(CONTEXT, LOGLEVEL, ARGS...)
#define DLT_UINT(UINT_VAR)
#define BUFFER_SIZE
Defines the buffer size of a single file package which will be logged to dlt.
void dlt_user_log_file_errorMessage(DltContext *fileContext, const char *filename, int errorCode)
static char str[DLT_DAEMON_TEXTBUFSIZE]
Definition: dlt-daemon.c:80
#define DLT_FILETRANSFER_ERROR_INFO_ABOUT
#define DLT_FILETRANSFER_ERROR_FILE_END
#define DLT_FILETRANSFER_ERROR_FILE_HEAD
#define DLT_FILETRANSFER_TRANSFER_ALL_PACKAGES
#define DLT_FILETRANSFER_ERROR_FILE_COMPLETE1
#define NULL
Definition: dlt_common.h:232