Added missing license header to two files
[profile/ivi/dlt-daemon.git] / src / examples / dlt-example-filetransfer.c
1 /*
2  * Dlt Test Client - Diagnostic Log and Trace
3  * @licence app begin@
4  *
5  * Copyright (C) 2011, BMW AG - Alexander Wenzel <alexander.wenzel@bmw.de>
6  * 
7  * This program is free software; you can redistribute it and/or modify it under the terms of the 
8  * GNU Lesser General Public License, version 2.1, as published by the Free Software Foundation.
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even 
10  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General 
11  * Public License, version 2.1, for more details.
12  * 
13  * You should have received a copy of the GNU Lesser General Public License, version 2.1, along 
14  * with this program; if not, see <http://www.gnu.org/licenses/lgpl-2.1.html>.
15  * 
16  * Note that the copyright holders assume that the GNU Lesser General Public License, version 2.1, may 
17  * also be applicable to programs even in cases in which the program is not a library in the technical sense.
18  * 
19  * Linking DLT statically or dynamically with other modules is making a combined work based on DLT. You may 
20  * license such other modules under the GNU Lesser General Public License, version 2.1. If you do not want to 
21  * license your linked modules under the GNU Lesser General Public License, version 2.1, you 
22  * may use the program under the following exception.
23  * 
24  * As a special exception, the copyright holders of DLT give you permission to combine DLT 
25  * with software programs or libraries that are released under any license unless such a combination is not
26  * permitted by the license of such a software program or library. You may copy and distribute such a 
27  * system following the terms of the GNU Lesser General Public License, version 2.1, including this
28  * special exception, for DLT and the licenses of the other code concerned.
29  * 
30  * Note that people who make modified versions of DLT are not obligated to grant this special exception 
31  * for their modified versions; it is their choice whether to do so. The GNU Lesser General Public License, 
32  * version 2.1, gives permission to release a modified version without this exception; this exception 
33  * also makes it possible to release a modified version which carries forward this exception.
34  *
35  * @licence end@
36  */
37
38
39 /*******************************************************************************
40 **                                                                            **
41 **  SRC-MODULE: dlt-test-client.c                                             **
42 **                                                                            **
43 **  TARGET    : linux                                                         **
44 **                                                                            **
45 **  PROJECT   : DLT                                                           **
46 **                                                                            **
47 **  AUTHOR    : Alexander Wenzel Alexander.AW.Wenzel@bmw.de                   **
48 **                                                                            **
49 **  PURPOSE   :                                                               **
50 **                                                                            **
51 **  REMARKS   :                                                               **
52 **                                                                            **
53 **  PLATFORM DEPENDANT [yes/no]: yes                                          **
54 **                                                                            **
55 **  TO BE CHANGED BY USER [yes/no]: no                                        **
56 **                                                                            **
57 *******************************************************************************/
58
59 /*******************************************************************************
60 **                      Author Identity                                       **
61 ********************************************************************************
62 **                                                                            **
63 ** Initials     Name                       Company                            **
64 ** --------     -------------------------  ---------------------------------- **
65 **  aw          Alexander Wenzel           BMW                                **
66 *******************************************************************************/
67
68
69 #include <stdlib.h>
70 #include <stdio.h>
71 #include <ctype.h>
72
73 #include <dlt_filetransfer.h>   /*Needed for transferring files with the dlt protocol*/
74 #include <dlt.h>                        /*Needed for dlt logging*/
75
76
77 #define MAXSTRLEN 1024
78
79 #define FLTR_APP_DESC      "Filetransfer application"
80 #define FLTR_CONTEXT_DESC  "Filetransfer context"
81
82 #define FLTR_APP "FLTR"
83 #define FLTR_CONTEXT "FLTR"
84
85 #define TIMEOUT 1
86
87 //!Declare some context for the file transfer. It's not a must have to do this, but later you can set a filter on this context in the dlt viewer.
88 DLT_DECLARE_CONTEXT(fileContext);
89
90
91 /**
92  * Print usage information of tool.
93  */
94 void usage()
95 {
96         char version[255];
97
98         dlt_get_version(version);
99
100     printf("Usage: dlt-example-filetransfer [options] <command>\n");
101     printf("Filetransfer example");
102     printf("%s \n", version);
103         printf("Command:\n");
104         printf("-f file      - File to transfer (absolute path)\n");
105     printf("Options:\n");
106     printf("-a apid      - Set application id to apid (default: FLTR)\n");
107     printf("-c ctid      - Set context id to ctid (default: FLTR)\n");
108     printf("-t ms        - Timeout between file packages in ms (minimum 1 ms)\n");
109     printf("-d           - Flag to delete the file after the transfer (default: false)\n"); 
110     printf("-i           - Flag to log file infos to DLT before transfer file (default: false)\n");  
111     printf("-h           - This help\n");
112
113 }
114
115
116 //!Main program dlt-test-filestransfer starts here
117 int main(int argc, char* argv[])
118 {       
119         //char str[MAXSTRLEN];
120     int opt, timeout, dltResult;;
121
122     char apid[DLT_ID_SIZE];
123     char ctid[DLT_ID_SIZE];
124     
125     //char version[255];
126
127         int dflag = 0;
128         int iflag = 0;
129         char *fvalue = 0;
130         char *tvalue = 0;
131
132     dlt_set_id(apid, FLTR_APP);
133     dlt_set_id(ctid, FLTR_CONTEXT);
134
135         while ((opt = getopt(argc, argv, "idf:t:a:c:h")) != -1)
136     {
137         switch (opt)
138         {
139                 case 'd':
140         {
141             dflag = 1;
142             break;
143         }
144         case 'i':
145         {
146             iflag = 1;
147             break;
148         }
149         case 'f':
150         {
151             fvalue = optarg;
152             break;
153         }
154         case 't':
155         {
156             tvalue = optarg;
157             break;
158         }
159         case 'a':
160         {
161             dlt_set_id(apid,optarg);
162             break;
163         }
164         case 'c':
165         {
166             dlt_set_id(ctid,optarg);
167             break;
168         }
169         case 'h':
170         {
171             usage();
172             break;
173         }
174         case '?':
175         {
176             if (optopt == 'a' || optopt == 'c' || optopt == 'f' || optopt == 't')
177             {
178                 fprintf (stderr, "Option -%c requires an argument.\n", optopt);
179             }
180             else if (isprint (optopt))
181             {
182                 fprintf (stderr, "Unknown option `-%c'.\n", optopt);
183             }
184             else
185             {
186                 fprintf (stderr, "Unknown option character `\\x%x'.\n",optopt);
187             }
188             /* unknown or wrong option used, show usage information and terminate */
189             usage();
190             return -1;
191         }
192         }
193     }
194         
195         if (tvalue)
196     {
197         timeout = atoi(tvalue);
198     }
199     else
200     {
201         timeout = TIMEOUT;
202     }
203
204         if (!fvalue)
205         {
206                 usage();
207                 return -1;
208         }
209
210         //Register the application at the dlt-daemon
211         dltResult = DLT_REGISTER_APP(apid,FLTR_APP_DESC);
212         
213         //Register the context of the main program at the dlt-daemon
214         dltResult = DLT_REGISTER_CONTEXT(fileContext,ctid,FLTR_CONTEXT_DESC);
215         
216         //More details in corresponding methods 
217         if( dltResult == 0 ){
218                 if( iflag )
219                 {
220                         dlt_user_log_file_infoAbout(&fileContext,fvalue);
221                 }
222                 
223                 if( dlt_user_log_file_complete(&fileContext,fvalue,dflag,timeout) < 0 )
224                 {
225                         printf("File couldn't be transferred. Please check the dlt log messages.\n");
226                 }
227                 
228         } 
229         else
230         {
231                 printf("Filetransfer didn't start...error with dlt daemon. Please check the daemon.\n");
232         }
233         
234         //Unregister the context in which the file transfer happened from the dlt-daemon
235         DLT_UNREGISTER_CONTEXT(fileContext);
236         //Unregister the context of the main program from the dlt-daemon
237         DLT_UNREGISTER_APP();
238         
239         return 0;
240 }