1 #include "dlt_filetransfer.h"
3 //!Defines the buffer size of a single file package which will be logged to dlt
4 #define BUFFER_SIZE 1024
6 //!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.
10 #define ERROR_FILE_COMPLETE -300
11 #define ERROR_FILE_COMPLETE1 -301
12 #define ERROR_FILE_COMPLETE2 -302
13 #define ERROR_FILE_COMPLETE3 -303
14 #define ERROR_FILE_HEAD -400
15 #define ERROR_FILE_DATA -500
16 #define ERROR_FILE_END -600
17 #define ERROR_INFO_ABOUT -700
18 #define ERROR_PACKAGE_COUNT -800
21 //!Buffer for dlt file transfer. The size is defined by BUFFER_SIZE
22 unsigned char buffer[BUFFER_SIZE];
25 //!Get some information about the file size of a file
26 /**See stat(2) for more informations.
27 * @param file Absolute file path
28 * @return Returns the size of the file (if it is a regular file or a symbolic link) in bytes.
30 unsigned long getFilesize(const char* file){
33 return (unsigned long)st.st_size;
36 //!Get some information about the file serial number of a file
37 /** See stat(2) for more informations.
38 * @param file Absolute file path
39 * @return Returns a unique number associated with each filename
41 unsigned long getFileSerialNumber(const char* file){
44 return (unsigned long)st.st_ino;
47 //!Returns the creation date of a file
48 /** See stat(2) for more informations.
49 * @param file Absolute file path
50 * @return Returns the creation date of a file
52 time_t getFileCreationDate(const char* file){
58 //!Returns the creation date of a file
59 /** Format of the creation date is Day Mon dd hh:mm:ss yyyy
60 * @param file Absolute file path
61 * @return Returns the creation date of a file
63 char* getFileCreationDate2(const char* file){
67 struct tm *ts= localtime(&st.st_ctime);
71 //!Checks if the file exists
72 /**@param file Absolute file path
73 * @return Returns 1 if the file exists, 0 if the file does not exist
75 int isFile (const char* file)
78 return (stat (file, &st) == 0);
81 //!Waits a period of time
82 /**Waits a period of time. The minimal time to wait is MIN_TIMEOUT. This makes sure that the FIFO of dlt is not flooded.
83 * @param timeout Timeout to wait in seconds in ms but can not be smaller as MIN_TIMEOUT
85 void doTimeout(int timeout)
87 int total_size, used_size;
89 dlt_user_check_buffer(&total_size, &used_size);
91 /* sleep only if more than 50% of buffer used */
92 if((total_size - used_size) < (total_size/2))
94 printf("Wait %d of %d already used\n",used_size,total_size);
96 if(timeout>MIN_TIMEOUT)
98 usleep(timeout * 1000);
102 usleep(MIN_TIMEOUT * 1000);
107 //!Deletes the given file
109 * @param filename Absolute file path
110 * @return If the file is successfully deleted, a zero value is returned.If the file can not be deleted a nonzero value is returned.
112 int doRemoveFile(const char*filename){
113 return remove( filename);
116 void dlt_user_log_file_errorMessage(DltContext *fileContext, const char *filename, int errorCode){
120 DLT_LOG(*fileContext,DLT_LOG_ERROR,
124 DLT_UINT(getFileSerialNumber(filename)),
125 DLT_STRING(filename),
126 DLT_UINT(getFilesize(filename)),
127 DLT_STRING(getFileCreationDate2(filename)),
128 DLT_UINT(dlt_user_log_file_packagesCount(fileContext,filename)),
129 DLT_UINT(BUFFER_SIZE),
133 DLT_LOG(*fileContext,DLT_LOG_ERROR,
137 DLT_STRING(filename),
145 //!Logs specific file inforamtions to dlt
146 /**The filename, file size, file serial number and the number of packages will be logged to dlt.
147 * @param fileContext Specific context
148 * @param filename Absolute file path
149 * @return Returns 0 if everything was okey.If there was a failure a value < 0 will be returned.
151 int dlt_user_log_file_infoAbout(DltContext *fileContext, const char *filename){
155 DLT_LOG(*fileContext,DLT_LOG_INFO,
157 DLT_STRING("file serialnumber"),DLT_UINT(getFileSerialNumber(filename)),
158 DLT_STRING("filename"),DLT_STRING(filename),
159 DLT_STRING("file size in bytes"),DLT_UINT(getFilesize(filename)),
160 DLT_STRING("file creation date"),DLT_STRING(getFileCreationDate2(filename)),
161 DLT_STRING("number of packages"),DLT_UINT(dlt_user_log_file_packagesCount(fileContext, filename)),
166 dlt_user_log_file_errorMessage(fileContext,filename,ERROR_INFO_ABOUT);
167 return ERROR_INFO_ABOUT;
171 //!Transfer the complete file as several dlt logs.
172 /**This method transfer the complete file as several dlt logs. At first it will be checked that the file exist.
173 * In the next step some generic informations about the file will be logged to dlt.
174 * Now the header will be logged to dlt. See the method dlt_user_log_file_header for more informations.
175 * Then the method dlt_user_log_data will be called with the parameter to log all packages in a loop with some timeout.
176 * 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.
177 * @param fileContext Specific context to log the file to dlt
178 * @param filename Absolute file path
179 * @param deleteFlag Flag if the file will be deleted after transfer. 1->delete, 0->notDelete
180 * @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.
181 * @return Returns 0 if everything was okey. If there was a failure a value < 0 will be returned.
183 int dlt_user_log_file_complete(DltContext *fileContext, const char *filename, int deleteFlag, int timeout)
185 if(!isFile(filename))
187 dlt_user_log_file_errorMessage(fileContext,filename, ERROR_FILE_COMPLETE);
188 return ERROR_FILE_COMPLETE;
191 if(dlt_user_log_file_header(fileContext,filename) != 0)
193 return ERROR_FILE_COMPLETE1;
196 if(dlt_user_log_file_data(fileContext, filename,LONG_MAX,timeout) != 0)
198 return ERROR_FILE_COMPLETE2;
201 if(dlt_user_log_file_end(fileContext,filename, deleteFlag) != 0)
203 return ERROR_FILE_COMPLETE3;
209 //!This method gives information about the number of packages the file have
210 /**Every file will be divided into several packages. Every package will be logged as a single dlt log.
211 * The number of packages depends on the BUFFER_SIZE.
212 * At first it will be checked if the file exist. Then the file will be divided into
213 * several packages depending on the buffer size.
214 * @param fileContext Specific context to log the file to dlt
215 * @param filename Absolute file path
216 * @return Returns the number of packages if everything was okey. If there was a failure a value < 0 will be returned.
218 int dlt_user_log_file_packagesCount(DltContext *fileContext, const char *filename){
225 filesize = getFilesize(filename);
226 if(filesize < BUFFER_SIZE)
232 packages = filesize/BUFFER_SIZE;
234 if(filesize%BUFFER_SIZE == 0)
244 dlt_user_log_file_errorMessage(fileContext,filename,ERROR_PACKAGE_COUNT);
249 //!Transfer the head of the file as a dlt logs.
250 /**The head of the file must be logged to dlt because the head contains inforamtion about the file serial number,
251 * the file name, the file size, package number the file have and the buffer size.
252 * All these informations are needed from the plugin of the dlt viewer.
253 * See the Mainpages.c for more informations.
254 * @param fileContext Specific context to log the file to dlt
255 * @param filename Absolute file path
256 * @return Returns 0 if everything was okey. If there was a failure a value < 0 will be returned.
258 int dlt_user_log_file_header(DltContext *fileContext,const char *filename){
262 DLT_LOG(*fileContext,DLT_LOG_INFO,
264 DLT_UINT(getFileSerialNumber(filename)),
265 DLT_STRING(filename),
266 DLT_UINT(getFilesize(filename)),
267 DLT_STRING(getFileCreationDate2(filename));
268 DLT_UINT(dlt_user_log_file_packagesCount(fileContext,filename)),
269 DLT_UINT(BUFFER_SIZE),
277 dlt_user_log_file_errorMessage(fileContext,filename, ERROR_FILE_HEAD);
278 return ERROR_FILE_HEAD;
282 //!Transfer the content data of a file.
283 /**See the Mainpages.c for more informations.
284 * @param fileContext Specific context to log the file to dlt
285 * @param filename Absolute file path
286 * @param packageToTransfer Package number to transfer. If this param is LONG_MAX, the whole file will be transferred with a specific timeout
287 * @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.
288 * @return Returns 0 if everything was okey. If there was a failure a value < 0 will be returned.
290 int dlt_user_log_file_data(DltContext *fileContext,const char *filename, int packageToTransfer, int timeout){
298 file = fopen (filename,"rb");
301 dlt_user_log_file_errorMessage(fileContext,filename,ERROR_FILE_DATA);
302 return ERROR_FILE_DATA;
305 if( (packageToTransfer != LONG_MAX && packageToTransfer > dlt_user_log_file_packagesCount(fileContext,filename)) || packageToTransfer <= 0)
307 DLT_LOG(*fileContext,DLT_LOG_ERROR,
308 DLT_STRING("Error at dlt_user_log_file_data: packageToTransfer out of scope"),
309 DLT_STRING("packageToTransfer:"),
310 DLT_UINT(packageToTransfer),
311 DLT_STRING("numberOfMaximalPackages:"),
312 DLT_UINT(dlt_user_log_file_packagesCount(fileContext,filename)),
313 DLT_STRING("for File:"),
316 return ERROR_FILE_DATA;
321 if(packageToTransfer != LONG_MAX)
323 fseek ( file , (packageToTransfer-1)*BUFFER_SIZE , SEEK_SET );
324 readBytes = fread(buffer, sizeof(char), BUFFER_SIZE, file);
326 DLT_LOG(*fileContext,DLT_LOG_INFO,
328 DLT_UINT(getFileSerialNumber(filename)),
329 DLT_UINT(packageToTransfer),
330 DLT_RAW(buffer,readBytes),
334 //doTimeout(timeout);
338 while( !feof( file ) )
341 readBytes = fread(buffer, sizeof(char), BUFFER_SIZE, file);
343 DLT_LOG(*fileContext,DLT_LOG_INFO,
345 DLT_UINT(getFileSerialNumber(filename)),
347 DLT_RAW(buffer,readBytes),
351 //doTimeout(timeout);
360 dlt_user_log_file_errorMessage(fileContext,filename,ERROR_FILE_DATA);
361 return ERROR_FILE_DATA;
365 //!Transfer the end of the file as a dlt logs.
366 /**The end of the file must be logged to dlt because the end contains inforamtion about the file serial number.
367 * This informations is needed from the plugin of the dlt viewer.
368 * See the Mainpages.c for more informations.
369 * @param fileContext Specific context to log the file to dlt
370 * @param filename Absolute file path
371 * @param deleteFlag Flag to delete the file after the whole file is transferred (logged to dlt).1->delete,0->NotDelete
372 * @return Returns 0 if everything was okey. If there was a failure a value < 0 will be returned.
374 int dlt_user_log_file_end(DltContext *fileContext,const char *filename,int deleteFlag){
379 DLT_LOG(*fileContext,DLT_LOG_INFO,
381 DLT_UINT(getFileSerialNumber(filename)),
386 if( doRemoveFile(filename) != 0 ){
387 dlt_user_log_file_errorMessage(fileContext,filename,ERROR_FILE_END);
394 dlt_user_log_file_errorMessage(fileContext,filename,ERROR_FILE_END);
395 return ERROR_FILE_END;