automotive-dlt
dlt-logstorage-common.c
Go to the documentation of this file.
1 
25 /*******************************************************************************
26 ** **
27 ** SRC-MODULE: dlt-logstorage-common.c **
28 ** **
29 ** TARGET : linux **
30 ** **
31 ** PROJECT : DLT **
32 ** **
33 ** AUTHOR : Christoph Lipka clipka@jp.adit-jv.com **
34 ** Frederic Berat fberat@de.adit-jv.com **
35 ** PURPOSE : **
36 ** **
37 ** REMARKS : Code extracted from dlt-control-common.c and reworked. **
38 ** **
39 ** PLATFORM DEPENDANT [yes/no]: yes **
40 ** **
41 ** TO BE CHANGED BY USER [yes/no]: no **
42 ** **
43 *******************************************************************************/
44 
45 /*******************************************************************************
46 ** Author Identity **
47 ********************************************************************************
48 ** **
49 ** Initials Name Company **
50 ** -------- ------------------------- ---------------------------------- **
51 ** cl Christoph Lipka ADIT **
52 ** fb Frederic Berat ADIT **
53 *******************************************************************************/
54 #define pr_fmt(fmt) "Logstorage common: "fmt
55 
56 #include <errno.h>
57 #include <dirent.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62 #include <pthread.h>
63 #include <sys/types.h>
64 #include <sys/socket.h>
65 
66 #include "dlt_common.h"
67 #include "dlt_protocol.h"
68 #include "dlt_client.h"
69 
70 #include "dlt-control-common.h"
71 #include "dlt-logstorage-common.h"
72 
73 #ifdef DLT_LOGSTORAGE_CTRL_UDEV_ENABLE
74 #include "dlt-logstorage-udev.h"
75 #endif
76 
77 #include "dlt-logstorage-prop.h"
78 
79 static struct LogstorageOptions {
80  int event_type;
83  long timeout;
84 } g_options = {
86  .handler_type = CTRL_NOHANDLER,
87 };
88 
90 {
91  return g_options.handler_type;
92 }
93 
94 void set_handler_type(char *type)
95 {
97 
98  if (type && check_proprietary_handling(type))
99  {
101  }
102 }
103 
105 {
106  return g_options.event_type;
107 }
108 
109 void set_default_event_type(long type)
110 {
111  g_options.event_type = type;
112 }
113 
114 char *get_default_path(void)
115 {
116  return g_options.device_path;
117 }
118 
119 void set_default_path(char *path)
120 {
122 
123  if (path != NULL)
124  {
125  strncpy(g_options.device_path, path, DLT_MOUNT_PATH_MAX - 1);
126  }
127 }
128 
129 /* Used by the handlers */
131 
133 {
134  return &lctrl;
135 }
136 
138 {
139  return lctrl.callback;
140 }
141 
143 {
144  return lctrl.fd;
145 }
146 
152 {
153  switch (get_handler_type())
154  {
155  case CTRL_PROPRIETARY:
156  return dlt_logstorage_prop_init();
157  case CTRL_UDEV:
158  default:
159 #ifdef DLT_LOGSTORAGE_CTRL_UDEV_ENABLE
160  return dlt_logstorage_udev_init();
161 #else
162  return -1;
163 #endif
164  }
165 }
166 
172 {
173  switch (get_handler_type())
174  {
175  case CTRL_PROPRIETARY:
177  case CTRL_UDEV:
178  default:
179 #ifdef DLT_LOGSTORAGE_CTRL_UDEV_ENABLE
181 #else
182  return -1;
183 #endif
184  }
185 }
186 
197 {
198  struct dirent **files;
199  int n;
200  int i = 0;
201  int ret = 0;
202 
203  if ((mnt_point == NULL) || (mnt_point[0] == '\0'))
204  {
205  pr_error("Mount point missing.\n");
206  return ret;
207  }
208 
209  pr_verbose("Now scanning %s\n", mnt_point);
210 
211  n = scandir(mnt_point, &files, NULL, alphasort);
212 
213  if (n <= 0)
214  {
215  pr_error("Cannot read mounted directory\n");
216  return ret;
217  }
218 
219  do
220  {
221  pr_verbose("Checking %s.\n", files[i]->d_name);
222 
223  if (strncmp(files[i]->d_name, CONF_NAME, strlen(CONF_NAME)) == 0)
224  {
225  /* We found it ! */
226  pr_verbose("File found.\n");
227  ret = 1;
228  break;
229  }
230  }
231  while (++i < n);
232 
233  for (i = 0 ; i < n ; i++)
234  {
235  free(files[i]);
236  }
237 
238  free(files);
239  return ret;
240 }
241 
249 {
250  if (mnt_point == NULL)
251  {
252  pr_error("Given mount point is NULL\n");
253  return 0;
254  }
255 
256  if (access(mnt_point, W_OK) == 0)
257  {
258  return 1;
259  }
260 
261  return 0;
262 }
263 
273  int conn_type,
274  char *path)
275 {
276  DltServiceOfflineLogstorage *serv = NULL;
277 
278  if (path == NULL)
279  {
280  pr_error("Mount path is uninitialized: %s\n", path);
281  return NULL;
282  }
283 
284  pr_verbose("Sending event %d for %s.\n", conn_type, path);
285 
286  *body = calloc(1, sizeof(DltControlMsgBody));
287 
288  if (!*body)
289  {
290  pr_error("Not able to allocate memory for body.\n");
291  return *body;
292  }
293 
294  (*body)->data = calloc(1, sizeof(DltServiceOfflineLogstorage));
295 
296  if (!(*body)->data)
297  {
298  free(*body);
299  *body = NULL;
300  pr_error("Not able to allocate memory for body data.\n");
301  return NULL;
302  }
303 
304  (*body)->size = sizeof(DltServiceOfflineLogstorage);
305 
306  serv = (DltServiceOfflineLogstorage *) (*body)->data;
307 
308  serv->service_id = DLT_SERVICE_ID_OFFLINE_LOGSTORAGE;
309  serv->connection_type = conn_type;
310  /* mount_point is DLT_MOUNT_PATH_MAX + 1 long,
311  * and the memory is already zeroed.
312  */
313  strncpy(serv->mount_point, path, DLT_MOUNT_PATH_MAX);
314 
315  pr_verbose("Body is now ready.\n");
316 
317  return *body;
318 }
319 
327 int dlt_logstorage_send_event(int type, char *mount_point)
328 {
329  int ret = 0;
330  DltControlMsgBody *msg_body = NULL;
331 
332  /* mount_point is checked against NULL in the preparation */
333  if (!prepare_message_body(&msg_body, type, mount_point))
334  {
335  pr_error("Data for Dlt Message body is NULL\n");
336  return -1;
337  }
338 
339  ret = dlt_control_send_message(msg_body, get_timeout());
340 
341  free(msg_body->data);
342  free(msg_body);
343 
344  return ret;
345 }
char device_path[DLT_MOUNT_PATH_MAX]
static DltLogstorageCtrl lctrl
#define CONF_NAME
int dlt_logstorage_get_handler_fd(void)
void set_default_path(char *path)
static int dlt_logstorage_prop_deinit(void)
Clean-up proprietary connection.
#define DLT_SERVICE_ID_OFFLINE_LOGSTORAGE
Definition: dlt_protocol.h:201
DltLogstorageHandler get_handler_type(void)
DltLogstorageCtrl * get_logstorage_control(void)
int dlt_logstorage_check_config_file(char *mnt_point)
Search for config file in given mount point.
void * dlt_logstorage_get_handler_cb(void)
int dlt_logstorage_udev_deinit(void)
Clean-up the udev data.
#define DLT_MOUNT_PATH_MAX
Definition: dlt_common.h:332
void set_handler_type(char *type)
char * get_default_path(void)
static int dlt_logstorage_prop_init(void)
Initialize proprietary connection.
void set_default_event_type(long type)
DltLogstorageHandler handler_type
#define pr_error(fmt,...)
int get_default_event_type(void)
int dlt_control_send_message(DltControlMsgBody *body, int timeout)
Send a message to the daemon and wait for the asynchronous answer.
static struct LogstorageOptions g_options
int dlt_logstorage_send_event(int type, char *mount_point)
Send a logstorage event to DLT.
#define pr_verbose(fmt,...)
#define EVENT_MOUNTED
DltLogstorageHandler
int dlt_logstorage_deinit_handler(void)
Clean-up the handler based on configuration.
static int check_proprietary_handling(char *type)
Check whether user wants to use proprietary handler.
long get_timeout(void)
int dlt_logstorage_init_handler(void)
Initialized the handler based on configuration.
int dlt_logstorage_check_directory_permission(char *mnt_point)
Check if given mount point is writable.
int dlt_logstorage_udev_init(void)
Initialize the private data.
static DltControlMsgBody * prepare_message_body(DltControlMsgBody **body, int conn_type, char *path)
Prepares the body of the message to be send to DLT.
#define NULL
Definition: dlt_common.h:232