automotive-dlt
dlt_offline_logstorage.c
Go to the documentation of this file.
1 
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <limits.h>
26 #include <ctype.h>
27 #include <syslog.h>
28 #include <sys/stat.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <dirent.h>
32 #include <time.h>
33 
34 #include "dlt_offline_logstorage.h"
36 #include "dlt_config_file_parser.h"
37 
38 #define DLT_OFFLINE_LOGSTORAGE_FILTER_ERROR 1
39 #define DLT_OFFLINE_LOGSTORAGE_STORE_FILTER_ERROR 2
40 
41 #define GENERAL_BASE_NAME "General"
42 /* Hash map functions */
43 
44 static int dlt_logstorage_hash_create(int num_entries, struct hsearch_data *htab)
45 {
46  memset(htab, 0, sizeof(*htab));
47 
48  if (hcreate_r(num_entries, htab) == 0)
49  return -1;
50 
51  return 0;
52 }
53 
54 static int dlt_logstorage_hash_destroy(struct hsearch_data *htab)
55 {
56  hdestroy_r(htab);
57 
58  return 0;
59 }
60 
61 static int dlt_logstorage_hash_add(char *key, void *value, struct hsearch_data *htab)
62 {
63  ENTRY e, *ep;
64 
65  memset(&e, 0, sizeof(ENTRY));
66  e.key = key;
67  e.data = value;
68 
69  if (hsearch_r(e, ENTER, &ep, htab) == 0)
70  return -1;
71 
72  return 0;
73 }
74 
75 static void *dlt_logstorage_hash_find(char *key, struct hsearch_data *htab)
76 {
77  ENTRY e, *ep;
78 
79  e.key = key;
80 
81  if (hsearch_r(e, FIND, &ep, htab) != 0)
82  return ep->data;
83  else
84  return NULL;
85 }
86 
87 /* Configuration file parsing helper functions */
88 
89 int dlt_logstorage_count_ids(const char *str)
90 {
91 
92  if(str == NULL)
93  return -1;
94 
95  // delimiter is: ","
96  const char *p = str;
97  int i = 0;
98  int num = 1;
99 
100  while(p[i] != 0)
101  {
102  if (p[i] == ',')
103  num += 1;
104 
105  i++;
106  }
107 
108  return num;
109 }
110 
125 int dlt_logstorage_read_list_of_names(char **names, char *value)
126 {
127  int i = 0;
128  int y = 0;
129  int len = 0;
130  char *tok;
131  int num = 1;
132 
133  /* free, alloce'd memory sot store new apid/ctid */
134  if (*names != NULL)
135  {
136  free(*names);
137  *names = NULL;
138  }
139 
140  if (value == NULL)
141  {
142  return -1;
143  }
144 
145  len = strlen(value);
146 
147  if (len == 0)
148  {
149  return -1;
150  }
151 
152  /* count number of delimiters to get actual number off names */
153  num = dlt_logstorage_count_ids(value);
154 
155  /* need to alloc space for 5 chars, 4 for the name and "," and "\0" */
156  *(names) = (char *)calloc(num * 5, sizeof(char));
157 
158  tok = strtok(value, ",");
159 
160  i = 1;
161  while (tok != NULL)
162  {
163  len = strlen(tok);
164  len = DLT_OFFLINE_LOGSTORAGE_MIN(len, 4);
165 
166  strncpy((*names + y), tok, len);
167  if (num > 1 && i < num)
168  {
169  strncpy((*names + y + len), ",", 1);
170  }
171  y += len + 1;
172 
173  i++;
174  tok = strtok(NULL, ",");
175  }
176 
177  return 0;
178 }
179 
189 int dlt_logstorage_read_log_level(int *log_level, char *value)
190 {
191  if (value == NULL)
192  {
193  *log_level = 0;
194  return -1;
195  }
196 
197  if (strcmp(value, "DLT_LOG_FATAL") == 0)
198  {
199  *log_level = 1;
200  }
201  else if (strcmp(value, "DLT_LOG_ERROR") == 0)
202  {
203  *log_level = 2;
204  }
205  else if (strcmp(value, "DLT_LOG_WARN") == 0)
206  {
207  *log_level = 3;
208  }
209  else if (strcmp(value, "DLT_LOG_INFO") == 0)
210  {
211  *log_level = 4;
212  }
213  else if (strcmp(value, "DLT_LOG_DEBUG") == 0)
214  {
215  *log_level = 5;
216  }
217  else if (strcmp(value, "DLT_LOG_VERBOSE") == 0)
218  {
219  *log_level = 6;
220  }
221  else
222  {
223  *log_level = 0;
224  dlt_log(LOG_ERR, "Invalid log level \n");
225  return -1;
226  }
227  return 0;
228 }
229 
239 int dlt_logstorage_read_file_name(char **file_name, char *value)
240 {
241  int len;
242 
243  if (value == NULL || strcmp(value, "") == 0)
244  {
245  return -1;
246  }
247 
248  if (*file_name != NULL)
249  {
250  *file_name = NULL;
251  }
252 
253  len = strlen(value);
254 
255  /* do not allow the user to change directory by adding a path like ../../logfile */
256  if (strstr(value, "..") == NULL)
257  {
258  *file_name = calloc((len+1), sizeof(char));
259  strncpy(*file_name, value, len);
260  }
261  else
262  {
263  dlt_log(LOG_ERR, "Invalid filename, .. is not accepted due to security issues \n");
264  return -1;
265  }
266 
267  return 0;
268 }
269 
282 int dlt_logstorage_read_number(unsigned int *number, char *value)
283 {
284  int i = 0;
285  int len = 0;
286  unsigned long size = 0;
287 
288  if (value == NULL)
289  {
290  return -1;
291  }
292 
293  *number = 0;
294  len = strlen(value);
295 
296  /* check if string consists of digits only */
297  for (i = 0; i < len; i++)
298  {
299  if (isdigit(value[i] == 0))
300  {
301  dlt_log(LOG_ERR, "Invalid, is not a number \n");
302  return -1;
303  }
304  }
305 
306  size = strtoul(value, NULL, 10);
307 
308  if (size == 0 || size > UINT_MAX)
309  {
310  dlt_log(LOG_ERR, "Invalid, is not a number \n");
311  return -1;
312  }
313 
314  *number = (unsigned int) size;
315 
316  return 0;
317 }
318 
331 int dlt_logstorage_set_sync_strategy(int *strategy, char *value)
332 {
333  if (value == NULL || strategy == NULL)
334  {
335  return -1;
336  }
337 
338  if (strcasestr(value, "ON_MSG") != NULL)
339  {
340  *strategy = DLT_LOGSTORAGE_SYNC_ON_MSG;
341  dlt_log(LOG_DEBUG, "ON_MSG found, ignore other if added\n");
342  }
343  else /* ON_MSG not set, combination of cache based strategies possible */
344  {
345  if (strcasestr(value, "ON_DAEMON_EXIT") != NULL)
346  {
348  }
349 
350  if (strcasestr(value, "ON_DEMAND") != NULL)
351  {
352  *strategy |= DLT_LOGSTORAGE_SYNC_ON_DEMAND;
353  }
354 
355  if (strcasestr(value, "ON_DEVICE_DISCONNECT") != NULL)
356  {
358  }
359 
360  if (*strategy == 0)
361  {
362  dlt_log(LOG_WARNING, "Unknown sync strategies. Set default ON_MSG\n");
363  *strategy = DLT_LOGSTORAGE_SYNC_ON_MSG;
364  }
365  }
366  return 0;
367 }
368 
378 int dlt_logstorage_set_ecuid(char **ecuid, char *value)
379 {
380  int len;
381 
382  if (ecuid == NULL || value == NULL || value[0] == '\0')
383  {
384  return -1;
385  }
386 
387  if (*ecuid != NULL)
388  {
389  free(*ecuid);
390  *ecuid = NULL;
391  }
392 
393  len = strlen(value);
394  *ecuid = calloc((len+1), sizeof(char));
395  strncpy(*ecuid, value, len);
396 
397  return 0;
398 }
399 
425 int dlt_logstorage_create_keys(char *appids, char* ctxids, char **keys, int *num_keys)
426 {
427  int i,j;
428  int curr_key = 0;
429  int curr_len = 0;
430  int num_appids = 0;
431  int num_ctxids = 0;
432  char *aids = NULL;
433  char *cids = NULL;
434  char *save_aids = NULL;
435  char *save_cids = NULL;
436 
437  if ((*keys) != NULL)
438  {
439  free((*keys));
440  (*keys) = NULL;
441  }
442 
443  *num_keys = 0;
444 
445  if (appids == NULL || ctxids == NULL)
446  return -1;
447 
448  /* discard appid=.* and ctxid=.* */
449  if ( strncmp(appids, ".*", 2) == 0 && strncmp(ctxids, ".*", 2) == 0 )
450  {
451  dlt_log(LOG_ERR,"Error: Not allowed combination of wildcards\n");
452  return -1;
453  }
454 
455  aids = strdup(appids);
456 
457  cids = (char *) calloc(strlen(ctxids)+1, sizeof(char));
458  if (cids == NULL)
459  {
460  free(aids);
461  return -1;
462  }
463 
464  /* calculate number of keys */
465  num_appids = dlt_logstorage_count_ids(appids);
466  num_ctxids = dlt_logstorage_count_ids(ctxids);
467  *(num_keys) = num_appids * num_ctxids;
468 
469  /* alloc needed number of keys */
470  *(keys) = (char*) calloc(*num_keys * DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN, sizeof(char));
471  if (*(keys) == NULL)
472  {
473  free(aids);
474  free(cids);
475  return -1;
476  }
477 
478  /* store all combinations of appid:ctxid in keys */
479  for (i = 1; i <= num_appids; i++)
480  {
481  char *tok_aids = NULL;
482  char *tok_cids = NULL;
483 
484  if (num_appids > 1 && i == 1)
485  {
486  tok_aids = strtok_r(aids,",", &save_aids);
487  }
488  else if (num_appids > 1 && i > 0)
489  {
490  tok_aids = strtok_r(NULL, ",", &save_aids);
491  }
492  else
493  {
494  tok_aids = aids;
495  }
496 
497  for (j = 1; j <= num_ctxids; j++)
498  {
499  if (num_ctxids > 1 && j == 1)
500  {
501  save_cids = NULL;
502  memcpy(cids, ctxids, strlen(ctxids));
503  tok_cids = strtok_r(cids, ",", &save_cids);
504  }
505  else if (num_ctxids > 1 && j > 0)
506  {
507  tok_cids = strtok_r(NULL, ",", &save_cids);
508  }
509  else
510  {
511  tok_cids = ctxids;
512  }
513 
514  if (strncmp(tok_aids, ".*", 2) == 0) /* only context id matters */
515  {
516  char curr_str[10] = { 0 };
517  strncpy(curr_str, ":", 1);
518  curr_len = strlen(tok_cids);
519  strncat(curr_str, tok_cids, curr_len);
520  curr_len = strlen(curr_str);
521  strncpy((*keys + (curr_key * DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN)), curr_str, curr_len);
522  }
523  else if (strncmp(tok_cids,".*", 2) == 0) /* only application id matters*/
524  {
525  char curr_str[10] = { 0 };
526  curr_len = strlen(tok_aids);
527  strncpy(curr_str, tok_aids, curr_len);
528  strncat(curr_str, ":", 1);
529  curr_len = strlen(curr_str);
530  strncpy((*keys + (curr_key * DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN)), curr_str, curr_len);
531  }
532  else /* key is combination of both */
533  {
534  char curr_str[10] = { 0 };
535  curr_len = strlen(tok_aids);
536  strncpy(curr_str, tok_aids, curr_len);
537  strncat(curr_str, ":", 1);
538  curr_len = strlen(tok_cids);
539  strncat(curr_str, tok_cids, curr_len);
540  curr_len = strlen(curr_str);
541  strncpy((*keys + (curr_key * DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN)), curr_str, curr_len);
542  }
543  curr_key += 1;
544  }
545  }
546  free(aids);
547  free(cids);
548 
549  return 0;
550 }
551 
561 int dlt_logstorage_device_connected(DltLogStorage *handle, char *mount_point)
562 {
563  if((handle == NULL) || (mount_point == NULL))
564  {
565  dlt_log(LOG_ERR, "dlt_logstorage_device_connected Error : Handle error \n");
566  return -1;
567  }
568 
570  {
571  dlt_log(LOG_ERR, "dlt_logstorage_device_connected Error : Device already connected, \n");
572  dlt_log(LOG_ERR, "Send disconnect, connect request \n");
573 
575  handle,
577  }
578 
579  strncpy(handle->device_mount_point,mount_point,DLT_MOUNT_PATH_MAX);
581  handle->config_status = 0;
582  handle->write_errors = 0;
583  handle->num_filter_keys = 0;
584 
585  handle->config_data = NULL;
586  handle->filter_keys = NULL;
587 
588  return 0;
589 }
590 
600 void dlt_logstorage_free(DltLogStorage *handle, int reason)
601 {
602  int i=0;
603 
605 
606  for(i=0; i<handle->num_filter_keys; i++)
607  {
608  /* sync data if necessary */
609  /* ignore return value */
611  &(handle->config_data[i].data),
612  reason);
613 
614  free(handle->config_data[i].data.file_name);
615 
616  if (handle->config_data[i].data.ecuid != NULL)
617  {
618  free(handle->config_data[i].data.ecuid);
619  }
620 
621  if (handle->config_data[i].data.log != NULL)
622  {
623  fclose(handle->config_data[i].data.log);
624  }
625 
626  if (handle->config_data[i].data.cache != NULL)
627  {
628  free(handle->config_data[i].data.cache);
629  }
630 
632  while(n)
633  {
634  DltLogStorageFileList *n1 = n;
635  n = n->next;
636  free(n1->name);
637  free(n1);
638  }
639  }
640 
641  free(handle->config_data);
642  handle->config_data = NULL;
643 
644  free(handle->filter_keys);
645  handle->filter_keys = NULL;
646 }
647 
648 
660 {
661  if (handle == NULL)
662  return -1;
663 
664  /* If configuration loading was done, free it */
666  {
667  dlt_logstorage_free(handle, reason);
668  }
669 
670  /* Reset all device status */
671  memset(handle->device_mount_point,'\0', sizeof(char) * DLT_MOUNT_PATH_MAX);
673  handle->config_status = 0;
674  handle->write_errors = 0;
675  handle->num_filter_keys = 0;
676 
677  return 0;
678 }
679 
692 int dlt_logstorage_prepare_table(DltLogStorage *handle, char *appid, char *ctxid, DltLogStorageConfigData *tmp_data)
693 {
694  int ret = 0;
695  int num_keys = 0;
696  char *keys = NULL;
697  int idx = 0;
698 
699  /* Allocate memory for filters */
700  if(handle->config_data == NULL)
701  {
703  memset(handle->config_data, 0, (sizeof(DltLogStorageConfig) * DLT_OFFLINE_LOGSTORAGE_MAXFILTERS));
704 
705  handle->filter_keys = malloc(sizeof(char) * DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN * DLT_OFFLINE_LOGSTORAGE_MAXFILTERS);
706  memset(handle->filter_keys, 0, sizeof(char) * DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN * DLT_OFFLINE_LOGSTORAGE_MAXFILTERS);
707  }
708 
709  ret = dlt_logstorage_create_keys(appid, ctxid, &keys, &num_keys);
710  if (ret != 0)
711  {
712  dlt_log(LOG_ERR, "Not able to create keys for hash table\n");
713  return -1;
714  }
715 
716  /* hash_add */
717  for (idx=0; idx<num_keys; idx++)
718  {
719  DltLogStorageConfig *p_node = NULL;
720 
721  if (num_keys > (DLT_OFFLINE_LOGSTORAGE_MAXFILTERS - handle->num_filter_keys))
722  {
723  dlt_log(LOG_ERR, "MAX filters reached \n");
724  break;
725  }
726 
727  p_node = &(handle->config_data[handle->num_filter_keys]);
728 
729  strcpy(p_node->key, keys+(idx * DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN));
730  memcpy(&p_node->data, tmp_data, sizeof(DltLogStorageConfigData));
731  p_node->data.file_name = strdup(tmp_data->file_name);
732  if (tmp_data->ecuid != NULL)
733  {
734  p_node->data.ecuid = strdup(tmp_data->ecuid);
735  }
736  else
737  {
738  p_node->data.ecuid = NULL;
739  }
740  p_node->data.records = NULL;
741  p_node->data.log = NULL;
742  p_node->data.cache = NULL;
743 
744  if(dlt_logstorage_hash_add(p_node->key, &p_node->data, &(handle->config_htab)) != 0)
745  {
746  dlt_log(LOG_ERR, "Adding to hash table failed, returning failure\n");
747 
749 
750  free(keys);
751  return -1;
752  }
753  /* update filter keys and number of keys */
754  strncat(handle->filter_keys + handle->num_filter_keys * DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN, keys, strlen(keys));
755  handle->num_filter_keys += 1;
756  }
757  free(keys);
758  return 0;
759 }
760 
780 int dlt_logstorage_validate_filter_value(char *filter_key, char *filter_value,
781  char **appid, char **ctxid,
782  DltLogStorageConfigData *tmp_data)
783 {
784  int ret = -1;
785 
786  if (strncmp(filter_key, "LogAppName", strlen("LogAppName")) == 0)
787  {
788  ret = dlt_logstorage_read_list_of_names(appid, filter_value);
789  if (ret == 0)
791  }
792  else if (strncmp(filter_key, "ContextName", strlen("ContextName")) == 0)
793  {
794  ret = dlt_logstorage_read_list_of_names(ctxid, filter_value);
795  if (ret == 0)
797  }
798  else if (strncmp(filter_key, "LogLevel", strlen("LogLevel")) == 0)
799  {
800  ret = dlt_logstorage_read_log_level(&(tmp_data->log_level), filter_value);
801  if (ret == 0)
803  }
804  else if (strncmp(filter_key, "FileSize", strlen("FileSize")) == 0)
805  {
806  ret = dlt_logstorage_read_number(&(tmp_data->file_size), filter_value);
807  if (ret == 0)
809  }
810  else if (strncmp(filter_key, "File", strlen("File")) == 0)
811  {
812  ret = dlt_logstorage_read_file_name(&(tmp_data->file_name), filter_value);
813  if (ret == 0)
815  }
816  else if (strncmp(filter_key, "NOFiles", strlen("NOFiles")) == 0)
817  {
818  ret = dlt_logstorage_read_number(&(tmp_data->num_files), filter_value);
819  if (ret == 0)
821  }
822  else if (strncmp(filter_key, "SyncBehavior", strlen ("SyncBehavior")) == 0)
823  {
824  ret = dlt_logstorage_set_sync_strategy(&(tmp_data->sync), filter_value);
825  if (ret == 0)
826  {
828  }
829  }
830  else if (strncmp(filter_key, "EcuID", strlen("EcuID")) == 0)
831  {
832  ret = dlt_logstorage_set_ecuid(&(tmp_data->ecuid), filter_value);
833  if (ret == 0)
834  {
836  }
837  }
838  else
839  {
840  /* Invalid filter key */
841  ret = -1;
842  dlt_log(LOG_ERR, "Invalid filter key");
843  }
844 
845  if (ret == -1)
846  dlt_log(LOG_ERR, "Error in configuration file\n");
847 
848  return ret;
849 }
850 
861 {
862  int len = 0;
863  int idx = 0;
864 
865  if (name == NULL)
866  {
867  return -1;
868  }
869 
870  len = strlen(name);
871 
872  /* Check if section header is of format "FILTER" followed by a number */
873  if (strncmp(name,
876  {
877  for (idx=6; idx<len-1; idx++)
878  {
879  if (!isdigit(name[idx]))
880  {
881  return -1;
882  }
883  }
884  return 0;
885  }
886  return -1;
887 }
888 
890  int strategy)
891 {
892  if (config == NULL)
893  {
894  return;
895  }
896 
897  if (strategy == DLT_LOGSTORAGE_SYNC_ON_MSG) /* file based */
898  {
902  }
903  else /* cache based */
904  {
908  }
909 }
910 
911 /*Return :
912 DLT_OFFLINE_LOGSTORAGE_FILTER_ERROR - On filter properties or value is not valid
913 DLT_OFFLINE_LOGSTORAGE_STORE_FILTER_ERROR - On error while storing in hash table
914 */
915 
916 int dlt_daemon_setup_filter_properties(DltLogStorage *handle, DltConfigFile *config_file, char *sec_name)
917 {
918  /* Read and store filters */
919  int j = 0;
920  int ret = -1;
921  int is_filter_set = DLT_OFFLINE_LOGSTORAGE_FILTER_UNINIT;
922  char *appid = NULL;
923  char *ctxid = NULL;
924  DltLogStorageConfigData tmp_data;
925  int num_filter_keys = DLT_OFFLINE_LOGSTORAGE_MAX_KEY_NUM;
926  char value[DLT_CONFIG_FILE_ENTRY_MAX_LEN + 1] = {'\0'};
927  char *filter_section_key[DLT_OFFLINE_LOGSTORAGE_MAX_KEY_NUM] =
928  {
929  "LogAppName",
930  "ContextName",
931  "LogLevel",
932  "File",
933  "FileSize",
934  "NOFiles",
935  "SyncBehavior",
936  "EcuID"
937  };
938 
939  memset(&tmp_data, 0, sizeof(DltLogStorageConfigData));
940 
942 
943  for (j = 0; j < num_filter_keys; j++)
944  {
945  /* Get filter value for filter keys */
946  ret = dlt_config_file_get_value(config_file, sec_name, filter_section_key[j], value);
947 
948  /* only return an error when the failure occurred on a mandatory
949  * value. */
950  if (ret != 0 &&
951  strncmp(filter_section_key[j], "SyncBehavior", strlen(filter_section_key[j]))
952  != 0 &&
953  strncmp(filter_section_key[j], "EcuID", strlen(filter_section_key[j]))
954  != 0)
955  {
956  is_filter_set = DLT_OFFLINE_LOGSTORAGE_FILTER_UNINIT;
958  dlt_log(LOG_ERR, "dlt_logstorage_store_filters Error : Reading filter value failed\n");
959  break;
960  }
961 
962  /* Validate filter value */
963  if (ret == 0)
964  {
965  ret = dlt_logstorage_validate_filter_value(filter_section_key[j], value, &appid, &ctxid, &tmp_data);
966  if ((ret != -1) && DLT_OFFLINE_LOGSTORAGE_IS_FILTER_PRESENT(is_filter_set))
967  {
968  is_filter_set |= ret;
969  }
970  else
971  {
972  is_filter_set = DLT_OFFLINE_LOGSTORAGE_FILTER_UNINIT;
974  break;
975  }
976  }
977  else
978  {
979  if (tmp_data.sync <= DLT_LOGSTORAGE_SYNC_ON_MSG)
980  {
981  dlt_log(LOG_INFO,
982  "Sync strategy not given. Use default ON_MSG\n");
983  /* set default sync strategy */
984  tmp_data.sync = DLT_LOGSTORAGE_SYNC_ON_MSG;
985  }
986  }
987  }
988 
989  /* If all items of the filter is populated store them */
991  {
992  /* depending on the specified strategy set function pointers for
993  * prepare, write and sync */
994  dlt_logstorage_filter_set_strategy(&tmp_data, tmp_data.sync);
995 
996  ret = dlt_logstorage_prepare_table(handle, appid, ctxid, &tmp_data);
997  if (ret != 0)
998  {
999  dlt_log(LOG_ERR, "dlt_logstorage_store_filters Error : Storing filter values failed\n");
1001  }
1002  }
1003 
1004  if (appid != NULL)
1005  free(appid);
1006  if (ctxid != NULL)
1007  free(ctxid);
1008  if (tmp_data.file_name != NULL)
1009  free(tmp_data.file_name);
1010 
1011  if (tmp_data.ecuid != NULL)
1012  {
1013  free(tmp_data.ecuid);
1014  }
1015 
1016  return ret;
1017 }
1018 
1030 int dlt_logstorage_store_filters(DltLogStorage *handle, char *config_file_name)
1031 {
1032  char error_msg[DLT_DAEMON_TEXTBUFSIZE];
1034  int sec = 0;
1035  int num_sec = 0;
1036  int ret = 0;
1037  /* we have to make sure that this function returns success if atleast one
1038  * filter configuration is valid and stored */
1039  int valid = -1;
1040 
1041  config = dlt_config_file_init(config_file_name);
1042  if (config == NULL)
1043  {
1044  dlt_log(LOG_CRIT, "Failed to open filter configuration file\n");
1045  return -1;
1046  }
1047 
1048  dlt_config_file_get_num_sections(config, &num_sec);
1049 
1050  for (sec = 0; sec < num_sec; sec++)
1051  {
1052  char sec_name[DLT_CONFIG_FILE_ENTRY_MAX_LEN];
1053 
1054  if (dlt_config_file_get_section_name(config, sec, sec_name) == -1)
1055  {
1056  dlt_log(LOG_CRIT, "Failed to read section name\n");
1057  return -1;
1058  }
1059 
1060  if (strstr(sec_name, GENERAL_BASE_NAME) != NULL)
1061  {
1062  dlt_log(LOG_CRIT, "General configuration not supported \n");
1063  continue;
1064  }
1065  else if (dlt_logstorage_validate_filter_name(sec_name) == 0)
1066  {
1067  ret = dlt_daemon_setup_filter_properties(handle, config, sec_name);
1069  {
1070  break;
1071  }
1072  else if (ret == DLT_OFFLINE_LOGSTORAGE_FILTER_ERROR)
1073  {
1074  /* Continue reading next filter section */
1075  continue;
1076  }
1077  else
1078  {
1079  /* Filter properties read and stored successfuly */
1080  valid = 0;
1081  }
1082  }
1083  else /* unknown section */
1084  {
1085  snprintf(error_msg,
1087  "Unknown section: %s",
1088  sec_name);
1089  dlt_log(LOG_WARNING, error_msg);
1090  }
1091  }
1092 
1093  dlt_config_file_release(config);
1094 
1095  return valid;
1096 }
1097 
1113 {
1114  char config_file_name[PATH_MAX + 1] = {'\0'};
1115 
1116  /* Check if handle is NULL or already initialized or already configured */
1117  if ((handle == NULL) || (handle->connection_type != DLT_OFFLINE_LOGSTORAGE_DEVICE_CONNECTED))
1118  return -1;
1119 
1120  /* Check if this device config was already setup */
1122  {
1123  dlt_log(LOG_ERR, "dlt_logstorage_load_config Error : Device already configured\n");
1124  dlt_log(LOG_ERR, "Send disconnect, connect request to re-configure \n");
1125  return -1;
1126  }
1127 
1128  if(snprintf(config_file_name, PATH_MAX, "%s/%s",
1129  handle->device_mount_point,
1131  {
1132  dlt_log(LOG_ERR, "dlt_logstorage_load_config Error : Device already configured\n");
1133  dlt_log(LOG_ERR, "Send disconnect, connect request to re-configure \n");
1134  return -1;
1135  }
1136 
1138  {
1139  dlt_log(LOG_ERR, "dlt_logstorage_load_config Error : Hash creation failed\n");
1140  return -1;
1141  }
1142 
1143  if (dlt_logstorage_store_filters(handle, config_file_name) != 0)
1144  {
1145  dlt_log(LOG_ERR, "dlt_logstorage_load_config Error : Storing filters failed\n");
1146  return -1;
1147  }
1148 
1150 
1151  return 0;
1152 }
1153 
1166 {
1168 
1169  /* Check if handle is NULL,already initialized or already configured */
1170  if ((handle == NULL) || (handle->connection_type != DLT_OFFLINE_LOGSTORAGE_DEVICE_CONNECTED)
1172  {
1173  return -1;
1174  }
1175 
1176  config = (DltLogStorageConfigData *)dlt_logstorage_hash_find(key, &(handle->config_htab));
1177 
1178  return config->log_level;
1179 }
1180 
1195 DltLogStorageConfigData **dlt_logstorage_get_config(DltLogStorage *handle, char *apid, char *ctid, int *num_config)
1196 {
1198  DltLogStorageConfigData *ptr_config = NULL;
1199  char key[3][DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN]= {{'\0'},{'\0'},{'\0'}};
1200  int i=0;
1201  int apid_len = 0;
1202  int ctid_len = 0;
1203 
1204  /* Check if handle is NULL,already initialized or already configured */
1205  if ((handle == NULL) || (handle->connection_type != DLT_OFFLINE_LOGSTORAGE_DEVICE_CONNECTED)
1207  return NULL;
1208 
1209  if ((apid == NULL) || (ctid == NULL))
1210  return NULL;
1211 
1212  /* Check if num_config passed is non zero */
1213  if (*(num_config) !=0)
1214  *(num_config) = 0;
1215 
1216  /* Prepare possible keys with apid and ctid,
1217  Possible combinataions are apid: , :ctid and apid:ctid */
1218  apid_len = strlen(apid);
1219  if (apid_len > DLT_ID_SIZE)
1220  apid_len = DLT_ID_SIZE;
1221 
1222  strncpy(key[0], apid, apid_len);
1223  strncat(key[0], ":",1);
1224 
1225  ctid_len = strlen(ctid);
1226  if (ctid_len > DLT_ID_SIZE)
1227  ctid_len = DLT_ID_SIZE;
1228 
1229  strncpy(key[1], ":", 1);
1230  strncat(key[1], ctid, ctid_len);
1231 
1232  strncpy(key[2], apid, apid_len);
1233  strncat(key[2], ":", 1);
1234  strncat(key[2], ctid, ctid_len);
1235 
1236  config = (DltLogStorageConfigData **)calloc(3, sizeof(DltLogStorageConfigData *));
1237 
1238  if (config == NULL)
1239  return NULL;
1240 
1241  /* Search the list thrice with keys as -apid: , :ctid and apid:ctid */
1242  for (i=0; i<3; i++)
1243  {
1244  ptr_config = (DltLogStorageConfigData *)dlt_logstorage_hash_find(key[i], &(handle->config_htab));
1245  if (ptr_config != NULL)
1246  {
1247  config[*(num_config)] = ptr_config;
1248  *(num_config) += 1;
1249  }
1250  }
1251 
1252  if (*(num_config) == 0)
1253  {
1254  free(config);
1255  return NULL;
1256  }
1257 
1258  return config;
1259 }
1260 
1276  char *appid,
1277  char *ctxid,
1278  char *ecuid,
1279  int log_level,
1280  int *num)
1281 {
1283  int i = 0;
1284 
1285  /* filter on names: find DltLogStorageConfigData structures */
1286  config = dlt_logstorage_get_config(handle, appid, ctxid, num);
1287 
1288  if (config == NULL)
1289  {
1290  *num = 0;
1291  return NULL;
1292  }
1293 
1294  for (i = 0; i < *num; i++)
1295  {
1296  /* filter on log level */
1297  if (log_level > config[i]->log_level)
1298  {
1299  config[i] = NULL;
1300  continue;
1301  }
1302 
1303  /* filter on ECU id only if EcuID is set */
1304  if (config[i]->ecuid != NULL)
1305  {
1306  if (strncmp(ecuid, config[i]->ecuid, strlen(ecuid)) != 0)
1307  {
1308  config[i] = NULL;
1309  }
1310  }
1311  }
1312 
1313  return config;
1314 }
1315 
1331  DltLogStorageUserConfig *uconfig,
1332  unsigned char *data1,
1333  int size1,
1334  unsigned char *data2,
1335  int size2,
1336  unsigned char *data3,
1337  int size3)
1338 {
1340  int i = 0;
1341  int ret = 0;
1342  int num = 0;
1343  int err = 0;
1344  /* data2 contains DltStandardHeader, DltStandardHeaderExtra and
1345  * DltExtendedHeader. We are interested in ecuid, apid, ctid and loglevel */
1346  DltExtendedHeader *extendedHeader;
1347  DltStandardHeaderExtra *extraHeader;
1348  DltStandardHeader *standardHeader;
1349  int standardHeaderExtraLen = 0;
1350 
1351  int log_level = -1;
1352 
1353  if (handle == NULL || uconfig == NULL ||
1354  data1 == NULL || data2 == NULL || data3 == NULL ||
1357  {
1358  return 0;
1359  }
1360  /* Calculate real length of DltStandardHeaderExtra */
1361  standardHeader = (DltStandardHeader *)data2;
1362  standardHeaderExtraLen = sizeof(DltStandardHeaderExtra);
1363  if (!DLT_IS_HTYP_WEID(standardHeader->htyp))
1364  {
1365  standardHeaderExtraLen -= DLT_ID_SIZE;
1366  }
1367  if (!DLT_IS_HTYP_WSID(standardHeader->htyp))
1368  {
1369  standardHeaderExtraLen -= DLT_SIZE_WSID;
1370  }
1371  if (!DLT_IS_HTYP_WTMS(standardHeader->htyp))
1372  {
1373  standardHeaderExtraLen -= DLT_SIZE_WTMS;
1374  }
1375 
1376  extendedHeader = (DltExtendedHeader *)(data2 +
1377  sizeof(DltStandardHeader) +
1378  standardHeaderExtraLen);
1379  extraHeader = (DltStandardHeaderExtra *)(data2 + sizeof(DltStandardHeader));
1380 
1381  log_level = DLT_GET_MSIN_MTIN(extendedHeader->msin);
1382 
1383  /* check if log message need to be stored in a certain device based on
1384  * filter configuration */
1385  config = dlt_logstorage_filter(handle,
1386  extendedHeader->apid,
1387  extendedHeader->ctid,
1388  extraHeader->ecu,
1389  log_level,
1390  &num);
1391 
1392  if (config != NULL)
1393  {
1394  /* store log message in every found filter */
1395  while (i < num)
1396  {
1397  if(config[i] != NULL)
1398  {
1399  /* prepare log file (create and/or open)*/
1400  ret = config[i]->dlt_logstorage_prepare(config[i],
1401  uconfig,
1402  handle->device_mount_point,
1403  size1 + size2 + size3);
1404  if (ret == 0) /* log data (write) */
1405  {
1406  ret = config[i]->dlt_logstorage_write(config[i],
1407  data1,
1408  size1,
1409  data2,
1410  size2,
1411  data3,
1412  size3);
1413 
1414  if (ret == 0)
1415  {
1416  /* flush to be sure log is stored on device */
1417  ret = config[i]->dlt_logstorage_sync(config[i],
1419  if (ret != 0)
1420  {
1421  dlt_log(LOG_ERR,
1422  "dlt_logstorage_write: Unable to sync.\n");
1423  }
1424  }
1425  else
1426  {
1427  handle->write_errors += 1;
1428  if (handle->write_errors >=
1430  {
1431  err = -1;
1432  }
1433 
1434  dlt_log(LOG_ERR,
1435  "dlt_logstorage_write: Unable to write.\n");
1436  }
1437  }
1438  else
1439  {
1440  dlt_log(LOG_ERR,
1441  "dlt_logstorage_write: Unable to prepare.\n");
1442  }
1443  }
1444  i++;
1445  }
1446 
1447  free(config);
1448  }
1449 
1450  return err;
1451 }
1452 
1454 {
1455  int i = 0;
1456 
1457  if (handle == NULL)
1458  {
1459  return -1;
1460  }
1461 
1462  for (i=0; i<handle->num_filter_keys; i++)
1463  {
1464  /* sync data if necessary */
1465  /* ignore return value */
1466  if (handle->config_data[i].data.dlt_logstorage_sync(
1467  &(handle->config_data[i].data),
1469  {
1470 
1471  dlt_log(LOG_ERR,
1472  "dlt_logstorage_sync_caches: Sync failed."
1473  " Continue with next cache.\n");
1474  }
1475  }
1476 
1477  return 0;
1478 }
int dlt_config_file_get_section_name(const DltConfigFile *file, int num, char *name)
#define DLT_OFFLINE_LOGSTORAGE_FILTER_PRESENT
int dlt_logstorage_load_config(DltLogStorage *handle)
struct hsearch_data config_htab
#define DLT_ID_SIZE
Definition: dlt_common.h:204
int(* dlt_logstorage_write)(DltLogStorageConfigData *config, unsigned char *data1, int size1, unsigned char *data2, int size2, unsigned char *data3, int size3)
int dlt_logstorage_write(DltLogStorage *handle, DltLogStorageUserConfig *uconfig, unsigned char *data1, int size1, unsigned char *data2, int size2, unsigned char *data3, int size3)
int(* dlt_logstorage_sync)(DltLogStorageConfigData *config, int status)
DltKpiConfig config
Definition: dlt-kpi.c:37
int dlt_config_file_get_num_sections(const DltConfigFile *file, int *num)
#define DLT_OFFLINE_LOGSTORAGE_SIZE_INIT
static int dlt_logstorage_hash_create(int num_entries, struct hsearch_data *htab)
#define DLT_GET_MSIN_MTIN(msin)
Definition: dlt_protocol.h:109
#define DLT_CONFIG_FILE_ENTRY_MAX_LEN
DltConfigFile * dlt_config_file_init(char *file_name)
unsigned int connection_type
int dlt_logstorage_prepare_msg_cache(DltLogStorageConfigData *config, DltLogStorageUserConfig *file_config, char *dev_path, int log_msg_size)
static int dlt_logstorage_hash_destroy(struct hsearch_data *htab)
#define DLT_OFFLINE_LOGSTORAGE_SYNC_BEHAVIOR
DltLogStorageConfig * config_data
int dlt_logstorage_device_disconnected(DltLogStorage *handle, int reason)
DltLogStorageConfigData ** dlt_logstorage_filter(DltLogStorage *handle, char *appid, char *ctxid, char *ecuid, int log_level, int *num)
#define DLT_OFFLINE_LOGSTORAGE_CONFIG_SECTION
DltReturnValue dlt_log(int prio, char *s)
Definition: dlt_common.c:2029
int dlt_logstorage_sync_on_msg(DltLogStorageConfigData *config, int status)
#define DLT_OFFLINE_LOGSTORAGE_APP_INIT
int dlt_logstorage_validate_filter_value(char *filter_key, char *filter_value, char **appid, char **ctxid, DltLogStorageConfigData *tmp_data)
int dlt_logstorage_write_on_msg(DltLogStorageConfigData *config, unsigned char *data1, int size1, unsigned char *data2, int size2, unsigned char *data3, int size3)
int dlt_logstorage_create_keys(char *appids, char *ctxids, char **keys, int *num_keys)
DltLogStorageFileList * records
int dlt_logstorage_count_ids(const char *str)
void dlt_logstorage_free(DltLogStorage *handle, int reason)
int dlt_daemon_setup_filter_properties(DltLogStorage *handle, DltConfigFile *config_file, char *sec_name)
#define DLT_OFFLINE_LOGSTORAGE_IS_FILTER_PRESENT(A)
#define DLT_MOUNT_PATH_MAX
Definition: dlt_common.h:332
#define DLT_OFFLINE_LOGSTORAGE_FILTER_UNINIT
#define DLT_OFFLINE_LOGSTORAGE_STORE_FILTER_ERROR
#define DLT_OFFLINE_LOGSTORAGE_NAME_INIT
int dlt_logstorage_sync_msg_cache(DltLogStorageConfigData *config, int status)
int dlt_logstorage_prepare_on_msg(DltLogStorageConfigData *config, DltLogStorageUserConfig *file_config, char *dev_path, int log_msg_size)
static int dlt_logstorage_hash_add(char *key, void *value, struct hsearch_data *htab)
int dlt_logstorage_read_file_name(char **file_name, char *value)
#define DLT_OFFLINE_LOGSTORAGE_MAX_KEY_NUM
#define DLT_OFFLINE_LOGSTORAGE_DEVICE_DISCONNECTED
int dlt_logstorage_read_number(unsigned int *number, char *value)
#define DLT_OFFLINE_LOGSTORAGE_MAXFILTERS
#define DLT_OFFLINE_LOGSTORAGE_DEVICE_CONNECTED
int dlt_logstorage_read_log_level(int *log_level, char *value)
int dlt_logstorage_write_msg_cache(DltLogStorageConfigData *config, unsigned char *data1, int size1, unsigned char *data2, int size2, unsigned char *data3, int size3)
DltLogStorageConfigData ** dlt_logstorage_get_config(DltLogStorage *handle, char *apid, char *ctid, int *num_config)
char key[DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN]
#define DLT_IS_HTYP_WEID(htyp)
Definition: dlt_protocol.h:91
#define DLT_OFFLINE_LOGSTORAGE_LOG_LVL_INIT
int dlt_logstorage_store_filters(DltLogStorage *handle, char *config_file_name)
#define DLT_LOGSTORAGE_SYNC_ON_DEMAND
int dlt_config_file_get_value(const DltConfigFile *file, const char *section, const char *key, char *value)
#define DLT_IS_HTYP_WSID(htyp)
Definition: dlt_protocol.h:92
int dlt_logstorage_set_sync_strategy(int *strategy, char *value)
#define DLT_OFFLINE_LOGSTORAGE_MIN(A, B)
#define DLT_OFFLINE_LOGSTORAGE_CONFIG_FILE_NAME
char device_mount_point[DLT_MOUNT_PATH_MAX]
#define DLT_OFFLINE_LOGSTORAGE_MAX_WRITE_ERRORS
#define DLT_SIZE_WSID
Definition: dlt_common.h:207
int dlt_logstorage_read_list_of_names(char **names, char *value)
unsigned int config_status
DltLogStorageConfigData data
#define DLT_OFFLINE_LOGSTORAGE_ECUID
int dlt_logstorage_sync_caches(DltLogStorage *handle)
#define DLT_LOGSTORAGE_SYNC_ON_DEVICE_DISCONNECT
#define DLT_OFFLINE_LOGSTORAGE_MAX_KEY_LEN
int(* dlt_logstorage_prepare)(DltLogStorageConfigData *config, DltLogStorageUserConfig *file_config, char *dev_path, int log_msg_size)
#define DLT_LOGSTORAGE_SYNC_ON_ERROR
#define DLT_OFFLINE_LOGSTORAGE_NUM_INIT
int dlt_logstorage_validate_filter_name(char *name)
struct DltLogStorageFileList * next
#define DLT_SIZE_WTMS
Definition: dlt_common.h:208
#define DLT_LOGSTORAGE_SYNC_ON_DAEMON_EXIT
static char str[DLT_DAEMON_TEXTBUFSIZE]
Definition: dlt-daemon.c:80
#define DLT_OFFLINE_LOGSTORAGE_CTX_INIT
int dlt_logstorage_set_ecuid(char **ecuid, char *value)
#define DLT_OFFLINE_LOGSTORAGE_FILTER_INITIALIZED(A)
int dlt_logstorage_get_loglevel_by_key(DltLogStorage *handle, char *key)
#define DLT_OFFLINE_LOGSTORAGE_FILTER_ERROR
#define DLT_IS_HTYP_WTMS(htyp)
Definition: dlt_protocol.h:93
int dlt_logstorage_prepare_table(DltLogStorage *handle, char *appid, char *ctxid, DltLogStorageConfigData *tmp_data)
int dlt_logstorage_device_connected(DltLogStorage *handle, char *mount_point)
#define DLT_LOGSTORAGE_SYNC_ON_MSG
#define DLT_DAEMON_TEXTBUFSIZE
void dlt_logstorage_filter_set_strategy(DltLogStorageConfigData *config, int strategy)
void dlt_config_file_release(DltConfigFile *file)
#define DLT_OFFLINE_LOGSTORAGE_CONFIG_DONE
#define GENERAL_BASE_NAME
#define NULL
Definition: dlt_common.h:232
static void * dlt_logstorage_hash_find(char *key, struct hsearch_data *htab)