3807aaf63cc3e0dd205bfe370a2f5d881c4ce92d
[profile/ivi/dlt-daemon.git] / src / examples / dlt-example-filetransfer.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <ctype.h>
4
5 #include <dlt_filetransfer.h>   /*Needed for transferring files with the dlt protocol*/
6 #include <dlt.h>                        /*Needed for dlt logging*/
7
8
9 #define MAXSTRLEN 1024
10
11 #define FLTR_APP_DESC      "Filetransfer application"
12 #define FLTR_CONTEXT_DESC  "Filetransfer context"
13
14 #define FLTR_APP "FLTR"
15 #define FLTR_CONTEXT "FLTR"
16
17 #define TIMEOUT 1
18
19 //!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.
20 DLT_DECLARE_CONTEXT(fileContext);
21
22
23 /**
24  * Print usage information of tool.
25  */
26 void usage()
27 {
28         char version[255];
29
30         dlt_get_version(version);
31
32     printf("Usage: dlt-example-filetransfer [options] <command>\n");
33     printf("Filetransfer example");
34     printf("%s \n", version);
35         printf("Command:\n");
36         printf("-f file      - File to transfer (absolute path)\n");
37     printf("Options:\n");
38     printf("-a apid      - Set application id to apid (default: FLTR)\n");
39     printf("-c ctid      - Set context id to ctid (default: FLTR)\n");
40     printf("-t ms        - Timeout between file packages in ms (minimum 1 ms)\n");
41     printf("-d           - Flag to delete the file after the transfer (default: false)\n"); 
42     printf("-i           - Flag to log file infos to DLT before transfer file (default: false)\n");  
43     printf("-h           - This help\n");
44
45 }
46
47
48 //!Main program dlt-test-filestransfer starts here
49 int main(int argc, char* argv[])
50 {       
51         //char str[MAXSTRLEN];
52     int opt, timeout, dltResult;;
53
54     char apid[DLT_ID_SIZE];
55     char ctid[DLT_ID_SIZE];
56     
57     //char version[255];
58
59         int dflag = 0;
60         int iflag = 0;
61         char *fvalue = 0;
62         char *tvalue = 0;
63
64     dlt_set_id(apid, FLTR_APP);
65     dlt_set_id(ctid, FLTR_CONTEXT);
66
67         while ((opt = getopt(argc, argv, "idf:t:a:c:h")) != -1)
68     {
69         switch (opt)
70         {
71                 case 'd':
72         {
73             dflag = 1;
74             break;
75         }
76         case 'i':
77         {
78             iflag = 1;
79             break;
80         }
81         case 'f':
82         {
83             fvalue = optarg;
84             break;
85         }
86         case 't':
87         {
88             tvalue = optarg;
89             break;
90         }
91         case 'a':
92         {
93             dlt_set_id(apid,optarg);
94             break;
95         }
96         case 'c':
97         {
98             dlt_set_id(ctid,optarg);
99             break;
100         }
101         case 'h':
102         {
103             usage();
104             break;
105         }
106         case '?':
107         {
108             if (optopt == 'a' || optopt == 'c' || optopt == 'f' || optopt == 't')
109             {
110                 fprintf (stderr, "Option -%c requires an argument.\n", optopt);
111             }
112             else if (isprint (optopt))
113             {
114                 fprintf (stderr, "Unknown option `-%c'.\n", optopt);
115             }
116             else
117             {
118                 fprintf (stderr, "Unknown option character `\\x%x'.\n",optopt);
119             }
120             /* unknown or wrong option used, show usage information and terminate */
121             usage();
122             return -1;
123         }
124         }
125     }
126         
127         if (tvalue)
128     {
129         timeout = atoi(tvalue);
130     }
131     else
132     {
133         timeout = TIMEOUT;
134     }
135
136         if (!fvalue)
137         {
138                 usage();
139                 return -1;
140         }
141
142         //Register the application at the dlt-daemon
143         dltResult = DLT_REGISTER_APP(apid,FLTR_APP_DESC);
144         
145         //Register the context of the main program at the dlt-daemon
146         dltResult = DLT_REGISTER_CONTEXT(fileContext,ctid,FLTR_CONTEXT_DESC);
147         
148         //More details in corresponding methods 
149         if( dltResult == 0 ){
150                 if( iflag )
151                 {
152                         dlt_user_log_file_infoAbout(&fileContext,fvalue);
153                 }
154                 
155                 if( dlt_user_log_file_complete(&fileContext,fvalue,dflag,timeout) < 0 )
156                 {
157                         printf("File couldn't be transferred. Please check the dlt log messages.\n");
158                 }
159                 
160         } 
161         else
162         {
163                 printf("Filetransfer didn't start...error with dlt daemon. Please check the daemon.\n");
164         }
165         
166         //Unregister the context in which the file transfer happened from the dlt-daemon
167         DLT_UNREGISTER_CONTEXT(fileContext);
168         //Unregister the context of the main program from the dlt-daemon
169         DLT_UNREGISTER_APP();
170         
171         return 0;
172 }