automotive-dlt
dlt-adaptor-udp.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-adaptor-udp.c **
30 ** **
31 ** TARGET : linux **
32 ** **
33 ** PROJECT : DLT **
34 ** **
35 ** AUTHOR : Alexander Wenzel Alexander.AW.Wenzel@bmw.de **
36 ** Markus Klein **
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 ** mk Markus Klein Fraunhofer ESK **
56 *******************************************************************************/
57 
58 /*******************************************************************************
59 ** Revision Control History **
60 *******************************************************************************/
61 
62 /*
63  * $LastChangedRevision: 1670 $
64  * $LastChangedDate: 2011-04-08 15:12:06 +0200 (Fr, 08. Apr 2011) $
65  * $LastChangedBy$
66  */
67 
68 #include <stdio.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <unistd.h>
72 #include <signal.h>
73 #include <sys/types.h>
74 #include <sys/socket.h>
75 #include <netinet/in.h>
76 #include <errno.h>
77 
78 #include "dlt_common.h"
79 #include "dlt_user.h"
80 
81 /* Port number, to which the syslogd-ng sends its log messages */
82 #define RCVPORT 47111
83 
84 #define MAXSTRLEN 1024
85 
86 #define PU_DLT_APP_DESC "udp adaptor application"
87 #define PU_DLT_CONTEXT_DESC "udp adaptor context"
88 
89 #define PU_DLT_APP "UDPA"
90 #define PU_DLT_CONTEXT "UDPC"
91 
93 
94 int main(int argc, char* argv[])
95 {
96  int sock;
97  int bytes_read;
98  socklen_t addr_len;
99  int opt, port;
100  char recv_data[MAXSTRLEN];
101  struct sockaddr_in client_addr, server_addr;
102 
103  char apid[DLT_ID_SIZE];
104  char ctid[DLT_ID_SIZE];
105  char version[255];
106  int verbosity = DLT_LOG_INFO;
107 
108  dlt_set_id(apid, PU_DLT_APP);
109  dlt_set_id(ctid, PU_DLT_CONTEXT);
110 
111  port = RCVPORT;
112 
113  while ((opt = getopt(argc, argv, "a:c:hp:v:")) != -1)
114  {
115  switch (opt)
116  {
117  case 'a':
118  {
119  dlt_set_id(apid,optarg);
120  break;
121  }
122  case 'c':
123  {
124  dlt_set_id(ctid,optarg);
125  break;
126  }
127  case 'h':
128  {
129  dlt_get_version(version,255);
130 
131  printf("Usage: dlt-adaptor-udp [options]\n");
132  printf("Adaptor for forwarding received UDP messages to DLT daemon.\n");
133  printf("%s \n", version);
134  printf("Options:\n");
135  printf("-a apid - Set application id to apid (default: UDPA)\n");
136  printf("-c ctid - Set context id to ctid (default: UDPC)\n");
137  printf("-p - Set receive port number for UDP messages (default: %d) \n", port);
138  printf("-v verbosity level - Set verbosity level (Default: INFO, values: FATAL ERROR WARN INFO DEBUG VERBOSE)\n");
139  printf("-h - This help\n");
140  return 0;
141  break;
142  }
143  case 'p':
144  {
145  port = atoi(optarg);
146  break;
147  }
148  case 'v':
149  {
150  if(!strcmp(optarg, "FATAL"))
151  {
152  verbosity = DLT_LOG_FATAL;
153  break;
154  }
155  else if(!strcmp(optarg, "ERROR"))
156  {
157  verbosity = DLT_LOG_ERROR;
158  break;
159  }
160  else if(!strcmp(optarg, "WARN"))
161  {
162  verbosity = DLT_LOG_WARN;
163  break;
164  }
165  else if(!strcmp(optarg, "INFO"))
166  {
167  verbosity = DLT_LOG_INFO;
168  break;
169  }
170  else if(!strcmp(optarg, "DEBUG"))
171  {
172  verbosity = DLT_LOG_DEBUG;
173  break;
174  }
175  else if(!strcmp(optarg, "VERBOSE"))
176  {
177  verbosity = DLT_LOG_VERBOSE;
178  break;
179  } else
180  {
181  printf("Wrong verbosity level, setting to INFO. Accepted values are: FATAL ERROR WARN INFO DEBUG VERBOSE\n");
182  verbosity = DLT_LOG_INFO;
183  break;
184  }
185  break;
186  }
187  default: /* '?' */
188  {
189  fprintf(stderr, "Unknown option '%c'\n", optopt);
190  exit(3);
191  return 3;//for parasoft
192  }
193  }
194  }
195 
196 #ifdef DLT_USE_IPv6
197  if ((sock = socket(AF_INET6, SOCK_DGRAM, 0)) == -1)
198 #else
199  if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
200 #endif
201  {
202  perror("Socket");
203  exit(1);
204  }
205 
206 #ifdef DLT_USE_IPv6
207  server_addr.sin_family = AF_INET6;
208 #else
209  server_addr.sin_family = AF_INET;
210 #endif
211  server_addr.sin_port = htons(port);
212  server_addr.sin_addr.s_addr = INADDR_ANY;
213  bzero(&(server_addr.sin_zero), 8);
214 
215 
216  if (bind(sock, (struct sockaddr *)&server_addr,
217  sizeof(struct sockaddr)) == -1)
218  {
219  perror("Bind");
220  return -1;
221  }
222 
223  addr_len = sizeof(struct sockaddr);
224 
227 
228  while (1)
229  {
230  bytes_read = 0;
231 
232  bytes_read = recvfrom(sock, recv_data, MAXSTRLEN, 0,
233  (struct sockaddr *)&client_addr, &addr_len);
234 
235  if (bytes_read == -1)
236  {
237  if (errno == EINTR)
238  {
239  continue;
240  }
241  else
242  {
245  exit(1);
246  }
247  }
248 
249  recv_data[bytes_read] = '\0';
250 
251  if (bytes_read != 0)
252  {
253  DLT_LOG(mycontext, verbosity, DLT_STRING(recv_data));
254  }
255  }
256 
259 
260  return 0;
261 }
#define DLT_UNREGISTER_APP()
#define DLT_ID_SIZE
Definition: dlt_common.h:204
#define PU_DLT_CONTEXT
void dlt_set_id(char *id, const char *text)
Definition: dlt_common.c:324
#define PU_DLT_APP
int main(int argc, char *argv[])
Definition: dlt-control.c:276
#define PU_DLT_APP_DESC
#define DLT_REGISTER_APP(APPID, DESCRIPTION)
#define RCVPORT
#define DLT_STRING(TEXT)
#define DLT_REGISTER_CONTEXT(CONTEXT, CONTEXTID, DESCRIPTION)
#define DLT_UNREGISTER_CONTEXT(CONTEXT)
DLT_DECLARE_CONTEXT(mycontext)
#define DLT_LOG(CONTEXT, LOGLEVEL, ARGS...)
void dlt_get_version(char *buf, size_t size)
Definition: dlt_common.c:3239
DltContext mycontext
#define PU_DLT_CONTEXT_DESC
#define MAXSTRLEN