LCOV - code coverage report
Current view: top level - console/logstorage - dlt-logstorage-common.c (source / functions) Hit Total Coverage
Test: dlt_final_coverage.info Lines: 32 82 39.0 %
Date: 2024-12-06 04:41:42 Functions: 7 15 46.7 %

          Line data    Source code
       1             : /**
       2             :  * Copyright (C) 2013 - 2015  Advanced Driver Information Technology.
       3             :  * This code is developed by Advanced Driver Information Technology.
       4             :  * Copyright of Advanced Driver Information Technology, Bosch and DENSO.
       5             :  *
       6             :  * This file is part of COVESA Project Dlt - Diagnostic Log and Trace console apps.
       7             :  *
       8             :  *
       9             :  * \copyright
      10             :  * This Source Code Form is subject to the terms of the
      11             :  * Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with
      12             :  * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
      13             :  *
      14             :  *
      15             :  * \author Syed Hameed <shameed@jp.adit-jv.com> ADIT 2013 - 2015
      16             :  * \author Christoph Lipka <clipka@jp.adit-jv.com> ADIT 2015
      17             :  * \author Frederic Berat <fberat@de.adit-jv.com> ADIT 2015
      18             :  *
      19             :  * \file dlt-logstorage-common.c
      20             :  * For further information see http://www.covesa.org/.
      21             :  */
      22             : 
      23             : /*******************************************************************************
      24             : **                                                                            **
      25             : **  SRC-MODULE: dlt-logstorage-common.c                                       **
      26             : **                                                                            **
      27             : **  TARGET    : linux                                                         **
      28             : **                                                                            **
      29             : **  PROJECT   : DLT                                                           **
      30             : **                                                                            **
      31             : **  AUTHOR    : Christoph Lipka clipka@jp.adit-jv.com                         **
      32             : **              Frederic Berat fberat@de.adit-jv.com                          **
      33             : **  PURPOSE   :                                                               **
      34             : **                                                                            **
      35             : **  REMARKS   : Code extracted from dlt-control-common.c and reworked.        **
      36             : **                                                                            **
      37             : **  PLATFORM DEPENDANT [yes/no]: yes                                          **
      38             : **                                                                            **
      39             : **  TO BE CHANGED BY USER [yes/no]: no                                        **
      40             : **                                                                            **
      41             : *******************************************************************************/
      42             : 
      43             : /*******************************************************************************
      44             : **                      Author Identity                                       **
      45             : ********************************************************************************
      46             : **                                                                            **
      47             : ** Initials     Name                       Company                            **
      48             : ** --------     -------------------------  ---------------------------------- **
      49             : **  cl          Christoph Lipka            ADIT                               **
      50             : **  fb          Frederic Berat             ADIT                               **
      51             : *******************************************************************************/
      52             : #define pr_fmt(fmt) "Logstorage common: "fmt
      53             : 
      54             : #include <errno.h>
      55             : #include <dirent.h>
      56             : #include <stdio.h>
      57             : #include <stdlib.h>
      58             : #include <string.h>
      59             : #include <unistd.h>
      60             : #include <pthread.h>
      61             : #include <sys/types.h>
      62             : #include <sys/socket.h>
      63             : 
      64             : #include "dlt_common.h"
      65             : #include "dlt_protocol.h"
      66             : #include "dlt_client.h"
      67             : 
      68             : #include "dlt-control-common.h"
      69             : #include "dlt-logstorage-common.h"
      70             : 
      71             : #ifdef DLT_LOGSTORAGE_CTRL_UDEV_ENABLE
      72             : #   include "dlt-logstorage-udev.h"
      73             : #endif
      74             : 
      75             : #include "dlt-logstorage-prop.h"
      76             : 
      77             : static struct LogstorageOptions {
      78             :     int event_type; /**< EVENT_UNMOUNTING/EVENT_MOUNTED */
      79             :     char device_path[DLT_MOUNT_PATH_MAX]; /**< Default Mount path */
      80             :     DltLogstorageHandler handler_type; /**< be controlled by udev or prop */
      81             :     long timeout; /**< Default timeout */
      82             : } g_options = {
      83             :     .event_type = EVENT_MOUNTED,
      84             :     .handler_type = CTRL_NOHANDLER,
      85             : };
      86             : 
      87           4 : DltLogstorageHandler get_handler_type(void)
      88             : {
      89           4 :     return g_options.handler_type;
      90             : }
      91             : 
      92           0 : void set_handler_type(char *type)
      93             : {
      94           0 :     g_options.handler_type = CTRL_UDEV;
      95             : 
      96             :     if (type && check_proprietary_handling(type))
      97             :         g_options.handler_type = CTRL_PROPRIETARY;
      98           0 : }
      99             : 
     100           6 : int get_default_event_type(void)
     101             : {
     102           6 :     return g_options.event_type;
     103             : }
     104             : 
     105           2 : void set_default_event_type(long type)
     106             : {
     107           2 :     g_options.event_type = (int) type;
     108           2 : }
     109             : 
     110           2 : char *get_default_path(void)
     111             : {
     112           2 :     return g_options.device_path;
     113             : }
     114             : 
     115           2 : void set_default_path(char *path)
     116             : {
     117             :     memset(g_options.device_path, 0, DLT_MOUNT_PATH_MAX);
     118             : 
     119           2 :     if (path != NULL)
     120             :         strncpy(g_options.device_path, path, DLT_MOUNT_PATH_MAX - 1);
     121           2 : }
     122             : 
     123             : /* Used by the handlers */
     124             : static DltLogstorageCtrl lctrl;
     125             : 
     126           0 : DltLogstorageCtrl *get_logstorage_control(void)
     127             : {
     128           0 :     return &lctrl;
     129             : }
     130             : 
     131           0 : void *dlt_logstorage_get_handler_cb(void)
     132             : {
     133           0 :     return lctrl.callback;
     134             : }
     135             : 
     136           0 : int dlt_logstorage_get_handler_fd(void)
     137             : {
     138           0 :     return lctrl.fd;
     139             : }
     140             : 
     141             : /** @brief Initialized the handler based on configuration
     142             :  *
     143             :  * @return 0 on success, -1 otherwise.
     144             :  */
     145           0 : int dlt_logstorage_init_handler(void)
     146             : {
     147           0 :     switch (get_handler_type()) {
     148             :     case CTRL_PROPRIETARY:
     149             :         return dlt_logstorage_prop_init();
     150           0 :     case CTRL_UDEV:
     151             :     default:
     152             : #ifdef DLT_LOGSTORAGE_CTRL_UDEV_ENABLE
     153             :         return dlt_logstorage_udev_init();
     154             : #else
     155           0 :         return -1;
     156             : #endif
     157             :     }
     158             : }
     159             : 
     160             : /** @brief Clean-up the handler based on configuration
     161             :  *
     162             :  * @return 0 on success, -1 otherwise.
     163             :  */
     164           0 : int dlt_logstorage_deinit_handler(void)
     165             : {
     166           0 :     switch (get_handler_type()) {
     167             :     case CTRL_PROPRIETARY:
     168             :         return dlt_logstorage_prop_deinit();
     169           0 :     case CTRL_UDEV:
     170             :     default:
     171             : #ifdef DLT_LOGSTORAGE_CTRL_UDEV_ENABLE
     172             :         return dlt_logstorage_udev_deinit();
     173             : #else
     174           0 :         return -1;
     175             : #endif
     176             :     }
     177             : }
     178             : 
     179             : /** @brief Search for config file in given mount point
     180             :  *
     181             :  * The file is searched at the top directory. The function exits once it
     182             :  * founds it.
     183             :  *
     184             :  * @param mnt_point The mount point to check
     185             :  *
     186             :  * @return 1 if the file is found, 0 otherwise.
     187             :  */
     188           0 : int dlt_logstorage_check_config_file(char *mnt_point)
     189             : {
     190             :     struct dirent **files;
     191             :     int n;
     192             :     int i = 0;
     193             :     int ret = 0;
     194             : 
     195           0 :     if ((mnt_point == NULL) || (mnt_point[0] == '\0')) {
     196           0 :         pr_error("Mount point missing.\n");
     197           0 :         return ret;
     198             :     }
     199             : 
     200           0 :     pr_verbose("Now scanning %s\n", mnt_point);
     201             : 
     202           0 :     n = scandir(mnt_point, &files, NULL, alphasort);
     203             : 
     204           0 :     if (n <= 0) {
     205           0 :         pr_error("Cannot read mounted directory\n");
     206           0 :         return ret;
     207             :     }
     208             : 
     209             :     do {
     210           0 :         pr_verbose("Checking %s.\n", files[i]->d_name);
     211             : 
     212           0 :         if (strncmp(files[i]->d_name, CONF_NAME, strlen(CONF_NAME)) == 0) {
     213             :             /* We found it ! */
     214           0 :             pr_verbose("File found.\n");
     215             :             ret = 1;
     216             :             break;
     217             :         }
     218           0 :     } while (++i < n);
     219             : 
     220           0 :     for (i = 0; i < n; i++)
     221           0 :         free(files[i]);
     222             : 
     223           0 :     free(files);
     224           0 :     return ret;
     225             : }
     226             : 
     227             : /** @brief Check if given mount point is writable
     228             :  *
     229             :  * @param mnt_point The mount point to check
     230             :  *
     231             :  * @return 1 if the file is writable, 0 otherwise.
     232             :  */
     233           0 : int dlt_logstorage_check_directory_permission(char *mnt_point)
     234             : {
     235           0 :     if (mnt_point == NULL) {
     236           0 :         pr_error("Given mount point is NULL\n");
     237           0 :         return 0;
     238             :     }
     239             : 
     240           0 :     if (access(mnt_point, W_OK) == 0)
     241           0 :         return 1;
     242             : 
     243             :     return 0;
     244             : }
     245             : 
     246             : /** @brief Prepares the body of the message to be send to DLT
     247             :  *
     248             :  * @param body A pointer to the MsgBody structure pointer
     249             :  * @param conn_type The type of the event (Mounted/Unmounting)
     250             :  * @param path The mount point path.
     251             :  *
     252             :  * @return The body once built or NULL.
     253             :  */
     254           2 : static DltControlMsgBody *prepare_message_body(DltControlMsgBody **body,
     255             :                                                int conn_type,
     256             :                                                char *path)
     257             : {
     258             :     DltServiceOfflineLogstorage *serv = NULL;
     259             : 
     260           2 :     if (path == NULL) {
     261           0 :         pr_error("Mount path is uninitialized.\n");
     262           0 :         return NULL;
     263             :     }
     264             : 
     265           2 :     pr_verbose("Sending event %d for %s.\n", conn_type, path);
     266             : 
     267           2 :     *body = calloc(1, sizeof(DltControlMsgBody));
     268             : 
     269           2 :     if (!*body) {
     270           0 :         pr_error("Not able to allocate memory for body.\n");
     271           0 :         return *body;
     272             :     }
     273             : 
     274           2 :     (*body)->data = calloc(1, sizeof(DltServiceOfflineLogstorage));
     275             : 
     276           2 :     if (!(*body)->data) {
     277           0 :         free(*body);
     278           0 :         *body = NULL;
     279           0 :         pr_error("Not able to allocate memory for body data.\n");
     280           0 :         return NULL;
     281             :     }
     282             : 
     283           2 :     (*body)->size = sizeof(DltServiceOfflineLogstorage);
     284             : 
     285             :     serv = (DltServiceOfflineLogstorage *)(*body)->data;
     286             : 
     287           2 :     serv->service_id = DLT_SERVICE_ID_OFFLINE_LOGSTORAGE;
     288           2 :     serv->connection_type = (uint8_t) conn_type;
     289             :     /* mount_point is DLT_MOUNT_PATH_MAX + 1 long,
     290             :      * and the memory is already zeroed.
     291             :      */
     292           2 :     strncpy(serv->mount_point, path, DLT_MOUNT_PATH_MAX - 1);
     293             : 
     294           2 :     pr_verbose("Body is now ready.\n");
     295             : 
     296           2 :     return *body;
     297             : }
     298             : 
     299             : /** @brief Send a logstorage event to DLT
     300             :  *
     301             :  * @param type The type of the event (Mounted/Unmounting)
     302             :  * @param mount_point The mount point for this event
     303             :  *
     304             :  * @return 0 On success, -1 otherwise.
     305             :  */
     306           2 : int dlt_logstorage_send_event(int type, char *mount_point)
     307             : {
     308             :     int ret = 0;
     309           2 :     DltControlMsgBody *msg_body = NULL;
     310             : 
     311             :     /* mount_point is checked against NULL in the preparation */
     312           2 :     if (!prepare_message_body(&msg_body, type, mount_point)) {
     313           0 :         pr_error("Data for Dlt Message body is NULL\n");
     314           0 :         return -1;
     315             :     }
     316             : 
     317           2 :     ret = dlt_control_send_message(msg_body, get_timeout());
     318             : 
     319           2 :     free(msg_body->data);
     320           2 :     free(msg_body);
     321             : 
     322           2 :     return ret;
     323             : }

Generated by: LCOV version 1.14