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 Anitha.BA <anithaammaji.baggam@in.bosch.com> ADIT 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-list.c
20 : * For further information see http://www.covesa.org/.
21 : */
22 :
23 : /*******************************************************************************
24 : ** **
25 : ** SRC-MODULE: dlt-logstorage-list.c **
26 : ** **
27 : ** TARGET : linux **
28 : ** **
29 : ** PROJECT : DLT **
30 : ** **
31 : ** AUTHOR : Christoph Lipka clipka@jp.adit-jv.com **
32 : ** Anitha.B.A anithaammaji.baggam@in.bosch.com **
33 : ** Frederic Berat fberat@de.adit-jv.com **
34 : ** **
35 : ** PURPOSE : linked list implementation for storing the device info **
36 : ** **
37 : ** REMARKS : **
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 : ** BA Anitha ADIT **
52 : ** cl Christoph Lipka ADIT **
53 : ** fb Frederic Berat ADIT **
54 : *******************************************************************************/
55 : #define pr_fmt(fmt) "Log storage list: "fmt
56 :
57 : #include <stdio.h>
58 : #include <stdlib.h>
59 : #include <string.h>
60 : #include <unistd.h>
61 :
62 : #include "dlt_common.h"
63 : #include "dlt-control-common.h"
64 : #include "dlt-logstorage-common.h"
65 :
66 : static struct LogstorageDeviceInfo
67 : {
68 : char *dev_node; /**< The device node */
69 : char *mnt_point; /**< Mount point for this device */
70 : struct LogstorageDeviceInfo *prev; /**< Previous element of the list */
71 : struct LogstorageDeviceInfo *next; /**< Next element of the list */
72 : } *g_info;
73 :
74 : /** @brief Prints the device list in verbose mode
75 : *
76 : * This can be used to debug the behavior.
77 : * Therefore, it's only available in verbose mode.
78 : */
79 0 : void print_list()
80 : {
81 0 : struct LogstorageDeviceInfo *ptr = g_info;
82 0 : pr_verbose(" -------Device list-------\n");
83 :
84 0 : while (ptr != NULL) {
85 0 : pr_verbose("%p:\t[%s][%s] \n", ptr, ptr->dev_node, ptr->mnt_point);
86 0 : ptr = ptr->next;
87 : }
88 :
89 0 : pr_verbose(" -------Device list end-------\n\n");
90 :
91 0 : return;
92 : }
93 :
94 : /** @brief Find element in the list based on device node
95 : *
96 : * Allows to check whether a device is already in the list or
97 : * to find out the one to be removed.
98 : *
99 : * @param node The device node to look for
100 : *
101 : * @return The element of the list found, NULL either.
102 : */
103 0 : static struct LogstorageDeviceInfo *logstorage_find_dev_info(const char *node)
104 : {
105 0 : struct LogstorageDeviceInfo *ptr = g_info;
106 :
107 0 : if (!node)
108 : return NULL;
109 :
110 0 : pr_verbose("Looking for %s.\n", node);
111 :
112 0 : while (ptr != NULL) {
113 0 : if (strncmp(ptr->dev_node, node, DLT_MOUNT_PATH_MAX) == 0) {
114 0 : pr_verbose("%s found in %p.\n", node, ptr);
115 : break;
116 : }
117 : else {
118 0 : ptr = ptr->next;
119 : }
120 : }
121 :
122 : return ptr;
123 : }
124 :
125 : /** @brief Add new device in the list
126 : *
127 : * The device is only added if a configuration file has been found and
128 : * if it's not already in the list.
129 : *
130 : * @param node The device node to add
131 : * @param path The corresponding mount point path
132 : *
133 : * @return 0 on success, -1 in case of error.
134 : */
135 0 : int logstorage_store_dev_info(const char *node, const char *path)
136 : {
137 : struct LogstorageDeviceInfo *ptr = NULL;
138 : size_t path_len = 0;
139 :
140 0 : if ((node == NULL) || (path == NULL)) {
141 0 : pr_error("Invalid input\n");
142 0 : return -1;
143 : }
144 :
145 0 : if (logstorage_find_dev_info(node)) {
146 0 : pr_verbose("%s already in list.\n", node);
147 0 : print_list();
148 0 : return 0;
149 : }
150 :
151 0 : ptr = calloc(1, sizeof(struct LogstorageDeviceInfo));
152 :
153 0 : if (ptr == NULL) {
154 0 : pr_error("Node creation failed\n");
155 0 : return -1;
156 : }
157 :
158 0 : ptr->dev_node = strdup(node);
159 0 : path_len = strlen(path);
160 :
161 0 : if (path_len > DLT_MOUNT_PATH_MAX)
162 : path_len = (size_t)DLT_MOUNT_PATH_MAX;
163 :
164 0 : ptr->mnt_point = (char *)calloc(1, path_len + 1);
165 :
166 0 : if (ptr->mnt_point == NULL) {
167 0 : pr_error("memory allocation failed for mnt_point\n");
168 0 : free(ptr);
169 : ptr = NULL;
170 0 : return -1;
171 : }
172 :
173 0 : ptr->mnt_point[path_len] = '\0';
174 0 : memcpy(ptr->mnt_point, path, path_len);
175 :
176 : /* Put it on head */
177 0 : ptr->next = g_info;
178 :
179 0 : if (g_info)
180 0 : g_info->prev = ptr;
181 :
182 0 : g_info = ptr;
183 :
184 0 : pr_verbose("%s added to list.\n", node);
185 0 : print_list();
186 :
187 0 : return 0;
188 : }
189 :
190 : /** @brief Remove a device from the list
191 : *
192 : * If the device is removed from the list, the mount point
193 : * pointer is given back to the caller. That means that
194 : * he has to free it.
195 : *
196 : * @param node The device node to be removed
197 : *
198 : * @return the mount point if the node is found, NULL either.
199 : */
200 0 : char *logstorage_delete_dev_info(const char *node)
201 : {
202 : struct LogstorageDeviceInfo *del = NULL;
203 : char *ret = NULL;
204 :
205 0 : del = logstorage_find_dev_info(node);
206 :
207 0 : if (del == NULL) {
208 0 : pr_verbose("%s not found in list.\n", node);
209 0 : print_list();
210 0 : return ret;
211 : }
212 :
213 : /* Has to be freed by the caller */
214 0 : ret = del->mnt_point;
215 :
216 0 : if (del->prev)
217 0 : del->prev->next = del->next;
218 :
219 0 : if (del->next)
220 0 : del->next->prev = del->prev;
221 :
222 0 : if (del == g_info)
223 0 : g_info = g_info->next;
224 :
225 0 : free(del->dev_node);
226 0 : free(del);
227 :
228 0 : pr_verbose("%s removed from list.\n", node);
229 0 : print_list();
230 :
231 0 : return ret;
232 : }
|