automotive-dlt
dlt-test-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 
28 /*******************************************************************************
29 ** **
30 ** SRC-MODULE: dlt-test-client.c **
31 ** **
32 ** TARGET : linux **
33 ** **
34 ** PROJECT : DLT **
35 ** **
36 ** AUTHOR : Alexander Wenzel Alexander.AW.Wenzel@bmw.de **
37 ** **
38 ** PURPOSE : **
39 ** **
40 ** REMARKS : **
41 ** **
42 ** PLATFORM DEPENDANT [yes/no]: yes **
43 ** **
44 ** TO BE CHANGED BY USER [yes/no]: no **
45 ** **
46 *******************************************************************************/
47 
48 /*******************************************************************************
49 ** Author Identity **
50 ********************************************************************************
51 ** **
52 ** Initials Name Company **
53 ** -------- ------------------------- ---------------------------------- **
54 ** aw Alexander Wenzel BMW **
55 *******************************************************************************/
56 
57 
58 #include <dlt_filetransfer.h> /*Needed for transferring files with the dlt protocol*/
59 #include <dlt.h> /*Needed for dlt logging*/
60 
62 DLT_DECLARE_CONTEXT(mainContext)
63 
64 DLT_DECLARE_CONTEXT(fileContext)
66 
68 char *file1;
70 char *file2;
72 char *file3_1;
74 char *file3_2;
76 char *file3_3;
78 int i,countPackages, transferResult;
79 static int g_numFailed = 0;
80 
82 void printTestResultPositiveExpected(const char *function, int result){
83 
84  if(result >= 0){
85  printf("%s successful\n",function);
86  }
87  else
88  {
89  printf("%s failed\n",function);
90  g_numFailed++;
91  }
92 
93 }
94 
96 void printTestResultNegativeExpected(const char *function, int result){
97 
98  if(result < 0){
99  printf("%s successful\n",function);
100  }
101  else
102  {
103  printf("%s failed\n",function);
104  g_numFailed++;
105  }
106 
107 }
108 
111  //Just some log to the main context
112  DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Started testF1P1 - dlt_user_log_file_complete"),DLT_STRING(file1));
113 
114  //Here's the line where the dlt file transfer is called. The method call needs a context, the absolute file path, will the file be deleted after transfer and the timeout between the packages
115  transferResult = dlt_user_log_file_complete(&fileContext,file1,0,20);
116  if(transferResult < 0 )
117  {
118  printf("Error: dlt_user_log_file_complete\n");
119  return transferResult;
120  }
121  //Just some log to the main context
122  DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Finished testF1P1"),DLT_STRING(file1));
123 
124  printTestResultPositiveExpected(__FUNCTION__,transferResult);
125 
126  return transferResult;
127 }
128 
131  int total_size, used_size;
132 
133  //Get the information how many packages have the file
134  countPackages = dlt_user_log_file_packagesCount(&fileContext,file1);
135  if(countPackages < 0 )
136  {
137  printf("Error: dlt_user_log_file_packagesCount\n");
138  printTestResultPositiveExpected(__FUNCTION__,countPackages);
139  return -1;
140  }
141  //Just some log to the main context
142  DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Started testF1P2 - transfer single package"),DLT_STRING(file1));
143 
144  //Logs the header of the file transfer. For more details see Mainpage.c.
145  //The header gives information about the file serial number, filename (with absolute path), filesize, packages of file, buffer size
146  transferResult = dlt_user_log_file_header(&fileContext,file1);
147  if(transferResult >= 0)
148  {
149  //Loop to log all packages
150  for(i=1;i<=countPackages;i++)
151  {
152  dlt_user_check_buffer(&total_size, &used_size);
153  if((total_size - used_size) < (total_size/2))
154  {
155  printf("Error: dlt_user_log_file_data\n");
156  printTestResultPositiveExpected(__FUNCTION__,transferResult);
157  break;
158  }
159 
160  //Logs one single package to the file context
161  transferResult = dlt_user_log_file_data(&fileContext,file1,i,20);
162  if(transferResult < 0)
163  {
164  printf("Error: dlt_user_log_file_data\n");
165  printTestResultPositiveExpected(__FUNCTION__,transferResult);
166  return transferResult;
167  }
168  }
169 
170  //Logs the end of the file transfer. For more details see Mainpage.c
171  //The end gives just information about the file serial number but is needed to signal that the file transfer has correctly finished and needed for the file transfer plugin of the dlt viewer.
172  transferResult = dlt_user_log_file_end(&fileContext,file1,0);
173  if(transferResult < 0)
174  {
175  printf("Error: dlt_user_log_file_end\n");
176  printTestResultPositiveExpected(__FUNCTION__,transferResult);
177  return transferResult;
178  }
179  }
180  else
181  {
182  printf("Error: dlt_user_log_file_header\n");
183  printTestResultPositiveExpected(__FUNCTION__,transferResult);
184  return transferResult;
185  }
186 
187  //Just some log to main context
188  DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Finished testF1P2 - transfer single package"),DLT_STRING(file1));
189  printTestResultPositiveExpected(__FUNCTION__,transferResult);
190  return 0;
191 }
194  //Just some log to main context
195  DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Started testF2P1 - dlt_user_log_file_complete"),DLT_STRING(file2));
196 
197  //Here's the line where the dlt file transfer is called. The method call needs a context, the absolute file path, will the file be deleted after transfer and the timeout between the packages
198  transferResult = dlt_user_log_file_complete(&fileContext,file2,0,20);
199  if(transferResult < 0)
200  {
201  printf("Error: dlt_user_log_file_complete\n");
202  printTestResultPositiveExpected(__FUNCTION__,transferResult);
203  return transferResult;
204  }
205  //Just some log to main context
206  DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Finished testF2P1"),DLT_STRING(file2));
207  printTestResultPositiveExpected(__FUNCTION__,transferResult);
208  return transferResult;
209 }
210 
213  int total_size, used_size;
214 
215  //Get the information how many packages have the file
216  countPackages = dlt_user_log_file_packagesCount(&fileContext,file2);
217  if(countPackages < 0 )
218  {
219  printf("Error: dlt_user_log_file_packagesCount\n");
220  printTestResultPositiveExpected(__FUNCTION__,countPackages);
221  return -1;
222  }
223 
224  //Just some log to the main context
225  DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Started testF2P2 - transfer single package"),DLT_STRING(file2));
226 
227  //Logs the header of the file transfer. For more details see Mainpage.c.
228  //The header gives information about the file serial number, filename (with absolute path), filesize, packages of file, buffer size
229  transferResult = dlt_user_log_file_header(&fileContext,file2);
230  if( transferResult >= 0){
231 
232  //Loop to log all packages
233  for(i=1;i<=countPackages;i++)
234  {
235  dlt_user_check_buffer(&total_size, &used_size);
236  if((total_size - used_size) < (total_size/2))
237  {
238  printf("Error: dlt_user_log_file_data\n");
239  printTestResultPositiveExpected(__FUNCTION__,transferResult);
240  break;
241  }
242 
243  //Logs one single package to the file context
244  transferResult = dlt_user_log_file_data(&fileContext,file2,i,20);
245  if(transferResult < 0)
246  {
247  printf("Error: dlt_user_log_file_data\n");
248  printTestResultPositiveExpected(__FUNCTION__,transferResult);
249  return transferResult;
250  }
251  }
252 
253  //Logs the end of the file transfer. For more details see Mainpage.c
254  //The end gives just information about the file serial number but is needed to signal that the file transfer has correctly finished and needed for the file transfer plugin of the dlt viewer.
255  transferResult = dlt_user_log_file_end(&fileContext,file2,0);
256  if(transferResult < 0)
257  {
258  printf("Error: dlt_user_log_file_end\n");
259  printTestResultPositiveExpected(__FUNCTION__,transferResult);
260  return transferResult;
261  }
262  }
263  else
264  {
265  printf("Error: dlt_user_log_file_header\n");
266  printTestResultPositiveExpected(__FUNCTION__,transferResult);
267  return transferResult;
268  }
269  //Just some log to the main context
270  DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Finished testF2P2"),DLT_STRING(file2));
271  printTestResultPositiveExpected(__FUNCTION__,transferResult);
272  return 0;
273 }
274 
277 
278  //Just some log to the main context
279  DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Started testF3P1"),DLT_STRING(file3_1));
280 
281  //Here's the line where the dlt file transfer is called. The method call needs a context, the absolute file path, will the file be deleted after transfer and the timeout between the packages
282  transferResult = dlt_user_log_file_complete(&fileContext,file3_1,0,20);
283  if(transferResult < 0)
284  {
285  //Error expected because file doesn't exist
286  //printf("Error: dlt_user_log_file_complete\n");
287  //Just some log to the main context
288  DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Finished testF3P1"),DLT_STRING(file3_1));
289  printTestResultNegativeExpected(__FUNCTION__,transferResult);
290  return transferResult;
291  }
292  printTestResultNegativeExpected(__FUNCTION__,transferResult);
293  return transferResult;
294 }
295 
296 
299 
300  //Get the information how many packages have the file
301  countPackages = dlt_user_log_file_packagesCount(&fileContext,file3_2);
302  if(countPackages < 0 )
303  {
304  //Error expected because file doesn't exist
305  //printf("Error: dlt_user_log_file_packagesCount\n");
306  //Just some log to the main context
307  DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Finished testF3P1"),DLT_STRING(file3_2));
308  printTestResultNegativeExpected(__FUNCTION__,countPackages);
309  return -1;
310  }
311  //Just some log to the main context
312  DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Started testF3P1"),DLT_STRING(file3_2));
313 
314  //Logs the header of the file transfer. For more details see Mainpage.c.
315  //The header gives information about the file serial number, filename (with absolute path), filesize, packages of file, buffer size
316  transferResult = dlt_user_log_file_header(&fileContext,file3_2);
317  if( transferResult >= 0){
318 
319  //Loop to log all packages
320  for(i=1;i<=countPackages;i++)
321  {
322  //Logs one single package to the file context
323  transferResult = dlt_user_log_file_data(&fileContext,file3_2,i,20);
324  if(transferResult < 0)
325  {
326  printf("Error: dlt_user_log_file_data\n");
327  printTestResultNegativeExpected(__FUNCTION__,transferResult);
328  return transferResult;
329  }
330  }
331 
332  //Logs the end of the file transfer. For more details see Mainpage.c
333  //The end gives just information about the file serial number but is needed to signal that the file transfer has correctly finished and needed for the file transfer plugin of the dlt viewer.
334  transferResult = dlt_user_log_file_end(&fileContext,file3_2,0);
335  if(transferResult < 0)
336  {
337  printf("Error: dlt_user_log_file_end\n");
338  printTestResultNegativeExpected(__FUNCTION__,transferResult);
339  return transferResult;
340  }
341  }
342  printTestResultNegativeExpected(__FUNCTION__,transferResult);
343  return 0;
344 }
345 
346 
349 
350  //Just some log to the main context
351  DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Started testF3P2"),DLT_STRING(file3_3));
352 
353  //Here's the line where the dlt file file info is called. The method call logs some information to dlt about the file, filesize, file serial number and number of packages
354  transferResult = dlt_user_log_file_infoAbout(&fileContext,file3_3);
355  if(transferResult < 0)
356  {
357  //Error expected because file doesn't exist
358  //printf("Error: dlt_user_log_file_infoAbout\n");
359  //Just some log to the main context
360  DLT_LOG(mainContext,DLT_LOG_INFO,DLT_STRING("Finished testF3P2"),DLT_STRING(file3_3));
361  printTestResultNegativeExpected(__FUNCTION__,transferResult);
362  return transferResult;
363  }
364  printTestResultNegativeExpected(__FUNCTION__,transferResult);
365  return 0;
366 }
367 
369 int main(void)
370 {
371  //First file contains some text
372  file1 = "/usr/local/share/dlt-filetransfer/dlt-test-filetransfer-file";
373  //Second file is a picture
374  file2 = "/usr/local/share/dlt-filetransfer/dlt-test-filetransfer-image.png";
375  //Third file doesn't exist. Just to test the reaction when the file isn't available.
376  file3_1 = "dlt-test-filetransfer-doesntExist_1";
377  //Third file doesn't exist. Just to test the reaction when the file isn't available.
378  file3_2 = "dlt-test-filetransfer-doesntExist_2";
379  //Third file doesn't exist. Just to test the reaction when the file isn't available.
380  file3_3 = "dlt-test-filetransfer-doesntExist_3";
381 
382  //Register the application at the dlt-daemon
383  DLT_REGISTER_APP("FLTR","Test Application filetransfer");
384 
385  //Register the context of the main program at the dlt-daemon
386  DLT_REGISTER_CONTEXT(mainContext,"MAIN","Main context for filetransfer test");
387 
388  //Register the context in which the file transfer will be logged at the dlt-daemon
389  DLT_REGISTER_CONTEXT(fileContext,"FLTR","Test Context for filetransfer");
390 
391  //More details in corresponding methods
392  testFile1Run1();
393  testFile1Run2();
394  testFile2Run1();
395  testFile2Run2();
396  testFile3Run1();
397  testFile3Run2();
398  testFile3Run3();
399 
400  //Unregister the context in which the file transfer happened from the dlt-daemon
401  DLT_UNREGISTER_CONTEXT(fileContext);
402  //Unregister the context of the main program from the dlt-daemon
403  DLT_UNREGISTER_CONTEXT(mainContext);
404  //Unregister the app from the dlt-daemon
406 
407  return g_numFailed == 0 ? 0 : 1;
408 }
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_UNREGISTER_APP()
int testFile2Run2()
Test the file transfer with the condition that the transferred file is bigger as the file transfer bu...
void printTestResultNegativeExpected(const char *function, int result)
Prints the test result.
int main(void)
Main program dlt-test-filestransfer starts here.
int testFile2Run1()
Test the file transfer with the condition that the transferred file is bigger as the file transfer bu...
DLT_DECLARE_CONTEXT(mainContext)
Declare some context for the main program. It&#39;s a must have to do this, when you want to log with dlt...
int dlt_user_log_file_packagesCount(DltContext *fileContext, const char *filename)
This method gives information about the number of packages the file have.
int testFile3Run2()
Test the file transfer with the condition that the transferred file does not exist using single packa...
#define DLT_REGISTER_APP(APPID, DESCRIPTION)
DltReturnValue dlt_user_check_buffer(int *total_size, int *used_size)
Definition: dlt_user.c:4512
#define DLT_STRING(TEXT)
#define DLT_REGISTER_CONTEXT(CONTEXT, CONTEXTID, DESCRIPTION)
int dlt_user_log_file_header(DltContext *fileContext, const char *filename)
Transfer the head of the file as a dlt logs.
int testFile3Run3()
Logs some information about the file.
#define DLT_UNREGISTER_CONTEXT(CONTEXT)
int dlt_user_log_file_end(DltContext *fileContext, const char *filename, int deleteFlag)
Transfer the end of the file as a dlt logs.
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.
int testFile1Run2()
Test the file transfer with the condition that the transferred file is smaller as the file transfer b...
#define DLT_LOG(CONTEXT, LOGLEVEL, ARGS...)
int testFile3Run1()
Test the file transfer with the condition that the transferred file does not exist using dlt_user_log...
int testFile1Run1()
Test the file transfer with the condition that the transferred file is smaller as the file transfer b...