automotive-dlt
dlt_daemon_connection.c
Go to the documentation of this file.
1 /*
2  * @licence app begin@
3  * SPDX license identifier: MPL-2.0
4  *
5  * Copyright (C) 2015 Advanced Driver Information Technology.
6  * This code is developed by Advanced Driver Information Technology.
7  * Copyright of Advanced Driver Information Technology, Bosch and DENSO.
8  *
9  * This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
10  *
11  * This Source Code Form is subject to the terms of the
12  * Mozilla Public License (MPL), v. 2.0.
13  * If a copy of the MPL was not distributed with this file,
14  * You can obtain one at http://mozilla.org/MPL/2.0/.
15  *
16  * For further information see http://www.genivi.org/.
17  * @licence end@
18  */
19 
30 #include <errno.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 
36 #include <sys/socket.h>
37 #include <sys/syslog.h>
38 #include <sys/types.h>
39 
41 #include "dlt_daemon_connection.h"
44 #include "dlt-daemon.h"
45 #include "dlt-daemon_cfg.h"
46 #include "dlt_daemon_common.h"
47 #include "dlt_common.h"
48 #include "dlt_gateway.h"
49 #include "dlt_daemon_socket.h"
50 
52 
68  void *msg,
69  size_t msg_size)
70 {
72 
73  if ((conn != NULL) && (conn->receiver != NULL))
74  {
75  type = conn->type;
76  }
77 
78  switch (type)
79  {
81  if(write(conn->receiver->fd, msg, msg_size) > 0)
82  return DLT_DAEMON_ERROR_OK;
85  return dlt_daemon_socket_sendreliable(conn->receiver->fd, msg, msg_size);
86  default:
88  }
89 }
90 
106  void *data1,
107  int size1,
108  void *data2,
109  int size2,
110  int sendserialheader)
111 {
112  int ret = 0;
113 
114  if (con == NULL)
115  {
117  }
118 
119  if (sendserialheader)
120  {
121  ret = dlt_connection_send(con,
122  (void *)dltSerialHeader,
123  sizeof(dltSerialHeader));
124  }
125 
126  if ((data1 != NULL) && (ret == DLT_RETURN_OK ))
127  {
128  ret = dlt_connection_send(con, data1, size1);
129  }
130 
131  if ((data2 != NULL) && (ret == DLT_RETURN_OK))
132  {
133  ret = dlt_connection_send(con, data2, size2);
134  }
135 
136  return ret;
137 }
138 
152 {
153  while (current && !((1 << current->type) & type_mask))
154  {
155  current = current->next;
156  }
157 
158  return current;
159 }
160 
162 {
163  if (!con)
164  return;
165 
166  switch (con->type)
167  {
169  /* We rely on the gateway for clean-up */
170  break;
171  default:
172  (void) dlt_receiver_free(con->receiver);
173  free(con->receiver);
174  con->receiver = NULL;
175  break;
176  }
177 }
178 
194  DltConnectionType type,
195  int fd)
196 {
197  DltReceiver *ret = NULL;
198 
199  switch (type)
200  {
202  /* FALL THROUGH */
204  /* FALL THROUGH */
206  /* FALL THROUGH */
208  ret = calloc(1, sizeof(DltReceiver));
209  if (ret) {
211  }
212  break;
214  ret = calloc(1, sizeof(DltReceiver));
215  if (ret) {
217  }
218  break;
220  /* FALL THROUGH */
222  /* FALL THROUGH */
224 #ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
225  /* FALL THROUGH */
227 #endif
228  /* FALL THROUGH */
230  ret = calloc(1, sizeof(DltReceiver));
231  if (ret) {
233  }
234  break;
236  /* We rely on the gateway for init */
237  ret = dlt_gateway_get_connection_receiver(&daemon_local->pGateway, fd);
238  break;
239  default:
240  ret = NULL;
241  }
242 
243  return ret;
244 }
245 
258 {
259  void *ret = NULL;
261 
262  if (con)
263  {
264  type = con->type;
265  }
266 
267  switch (type)
268  {
271  break;
274  break;
277  break;
280  break;
283  break;
286  break;
287 #ifdef DLT_SYSTEMD_WATCHDOG_ENABLE
290  break;
291 #endif
294  break;
297  break;
300  break;
303  break;
304  default:
305  ret = NULL;
306  }
307 
308  return ret;
309 }
310 
321 {
322  to_destroy->id = 0;
323  close(to_destroy->receiver->fd);
325  /* connection pointer might be in epoll queue and used even after destroying
326  * it. To make sure it is not used anymore, connection type is invalidated */
327  to_destroy->type = DLT_CONNECTION_TYPE_MAX;
328  free(to_destroy);
329 }
330 
347  DltEventHandler *evh,
348  int fd,
349  int mask,
350  DltConnectionType type)
351 {
352  DltConnection *temp = NULL;
353 
354  if (fd < 0) {
355  /* Nothing to do */
356  return 0;
357  }
358 
359  if (dlt_event_handler_find_connection(evh, fd) != NULL)
360  {
361  /* No need for the same client to be registered twice
362  * for the same event.
363  * TODO: If another mask can be expected,
364  * we need it to update the epoll event here.
365  */
366  return 0;
367  }
368 
369  temp = (DltConnection *)malloc(sizeof(DltConnection));
370 
371  if (temp == NULL)
372  {
373  dlt_log(LOG_CRIT, "Allocation of client handle failed\n");
374  return -1;
375  }
376 
377  memset(temp, 0, sizeof(DltConnection));
378 
379  temp->receiver = dlt_connection_get_receiver(daemon_local, type, fd);
380  if (!temp->receiver) {
381  char local_str[DLT_DAEMON_TEXTBUFSIZE];
382  snprintf(local_str,
384  "Unable to get receiver from %d connection.\n",
385  type);
386 
387  dlt_log(LOG_CRIT, local_str);
388  free(temp);
389  return -1;
390  }
391 
392  /* We are single threaded no need for protection. */
393  temp->id = connectionId++;
394  if (!temp->id)
395  {
396  /* Skipping 0 */
397  temp->id = connectionId++;
398  }
399 
400  temp->type = type;
401  temp->status = ACTIVE;
402 
403  /* Now give the ownership of the newly created connection
404  * to the event handler, by registering for events.
405  */
406  return dlt_event_handler_register_connection(evh, daemon_local, temp, mask);
407 }
DltConnection * dlt_event_handler_find_connection(DltEventHandler *ev, int fd)
Find connection with a specific fd in the connection list.
struct DltConnection * next
STATIC int dlt_connection_send(DltConnection *conn, void *msg, size_t msg_size)
Generic sending function.
int dlt_gateway_process_gateway_timer(DltDaemon *daemon, DltDaemonLocal *daemon_local, DltReceiver *receiver, int verbose)
Definition: dlt_gateway.c:914
DltConnectionStatus status
int dlt_daemon_process_control_connect(DltDaemon *daemon, DltDaemonLocal *daemon_local, DltReceiver *receiver, int verbose)
Definition: dlt-daemon.c:1761
int dlt_daemon_process_one_s_timer(DltDaemon *daemon, DltDaemonLocal *daemon_local, DltReceiver *recv, int verbose)
int dlt_daemon_process_sixty_s_timer(DltDaemon *daemon, DltDaemonLocal *daemon_local, DltReceiver *recv, int verbose)
#define DLT_DAEMON_RCVBUFSIZESERIAL
#define DLT_DAEMON_ERROR_OK
Definition: dlt-daemon.h:170
DltReturnValue dlt_log(int prio, char *s)
Definition: dlt_common.c:2029
#define DLT_DAEMON_ERROR_UNKNOWN
Definition: dlt-daemon.h:171
int dlt_daemon_process_client_messages(DltDaemon *daemon, DltDaemonLocal *daemon_local, DltReceiver *receiver, int verbose)
Definition: dlt-daemon.c:1593
static DltConnectionId connectionId
uintptr_t DltConnectionId
int dlt_gateway_process_passive_node_messages(DltDaemon *daemon, DltDaemonLocal *daemon_local, DltReceiver *receiver, int verbose)
Definition: dlt_gateway.c:731
DltReturnValue dlt_receiver_init(DltReceiver *receiver, int fd, int buffersize)
Definition: dlt_common.c:2126
void * dlt_connection_get_callback(DltConnection *con)
Get the callback from a specific connection.
int dlt_daemon_process_systemd_timer(DltDaemon *daemon, DltDaemonLocal *daemon_local, DltReceiver *recv, int verbose)
STATIC DltReceiver * dlt_connection_get_receiver(DltDaemonLocal *daemon_local, DltConnectionType type, int fd)
Get the receiver structure associated to a connection.
void dlt_connection_destroy(DltConnection *to_destroy)
Destroys a connection.
int dlt_daemon_process_user_messages(DltDaemon *daemon, DltDaemonLocal *daemon_local, DltReceiver *receiver, int verbose)
Definition: dlt-daemon.c:1948
const char dltSerialHeader[DLT_ID_SIZE]
Definition: dlt_common.c:70
DltReceiver * dlt_gateway_get_connection_receiver(DltGateway *gateway, int fd)
Definition: dlt_gateway.c:710
int dlt_daemon_process_client_messages_serial(DltDaemon *daemon, DltDaemonLocal *daemon_local, DltReceiver *receiver, int verbose)
Definition: dlt-daemon.c:1681
int dlt_event_handler_register_connection(DltEventHandler *evhdl, DltDaemonLocal *daemon_local, DltConnection *connection, int mask)
Registers a connection for event handling and takes its ownership.
DltConnection * dlt_connection_get_next(DltConnection *current, int type_mask)
Get the next connection filtered with a type mask.
#define DLT_DAEMON_RCVBUFSIZESOCK
int dlt_daemon_socket_sendreliable(int sock, void *buffer, int message_size)
dlt_daemon_socket_sendreliable - sends data to socket with additional checks and resending functional...
int dlt_daemon_process_control_messages(DltDaemon *daemon, DltDaemonLocal *daemon_local, DltReceiver *receiver, int verbose)
Definition: dlt-daemon.c:1817
DltGateway pGateway
Definition: dlt-daemon.h:141
int dlt_connection_send_multiple(DltConnection *con, void *data1, int size1, void *data2, int size2, int sendserialheader)
Send up to two messages through a connection.
STATIC void dlt_connection_destroy_receiver(DltConnection *con)
#define DLT_DAEMON_RCVBUFSIZE
int dlt_daemon_process_client_connect(DltDaemon *daemon, DltDaemonLocal *daemon_local, DltReceiver *receiver, int verbose)
Definition: dlt-daemon.c:1477
#define STATIC
Definition: dlt_common.h:343
DltConnectionType type
#define DLT_DAEMON_TEXTBUFSIZE
int dlt_connection_create(DltDaemonLocal *daemon_local, DltEventHandler *evh, int fd, int mask, DltConnectionType type)
Creates a connection and registers it to the DltEventHandler.
#define NULL
Definition: dlt_common.h:232
DltReturnValue dlt_receiver_free(DltReceiver *receiver)
Definition: dlt_common.c:2153