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 : char* dev_node; /**< The device node */
68 : char* mnt_point; /**< Mount point for this device */
69 : struct LogstorageDeviceInfo* prev; /**< Previous element of the list */
70 : struct LogstorageDeviceInfo* next; /**< Next element of the list */
71 : }* g_info;
72 :
73 : /** @brief Prints the device list in verbose mode
74 : *
75 : * This can be used to debug the behavior.
76 : * Therefore, it's only available in verbose mode.
77 : */
78 0 : void print_list(void)
79 : {
80 0 : struct LogstorageDeviceInfo* ptr = g_info;
81 0 : pr_verbose(" -------Device list-------\n");
82 :
83 0 : while (ptr != NULL) {
84 0 : pr_verbose("%p:\t[%s][%s] \n", (void*)ptr, ptr->dev_node, ptr->mnt_point);
85 0 : ptr = ptr->next;
86 : }
87 :
88 0 : pr_verbose(" -------Device list end-------\n\n");
89 :
90 0 : return;
91 : }
92 :
93 : /** @brief Find element in the list based on device node
94 : *
95 : * Allows to check whether a device is already in the list or
96 : * to find out the one to be removed.
97 : *
98 : * @param node The device node to look for
99 : *
100 : * @return The element of the list found, NULL either.
101 : */
102 0 : static struct LogstorageDeviceInfo* logstorage_find_dev_info(const char* node)
103 : {
104 0 : struct LogstorageDeviceInfo* ptr = g_info;
105 :
106 0 : if (!node)
107 : return NULL;
108 :
109 0 : pr_verbose("Looking for %s.\n", node);
110 :
111 0 : while (ptr != NULL) {
112 0 : if (strncmp(ptr->dev_node, node, DLT_MOUNT_PATH_MAX) == 0) {
113 0 : pr_verbose("%s found in %p.\n", node, (void*)ptr);
114 : break;
115 : } else {
116 0 : ptr = ptr->next;
117 : }
118 : }
119 :
120 : return ptr;
121 : }
122 :
123 : /** @brief Add new device in the list
124 : *
125 : * The device is only added if a configuration file has been found and
126 : * if it's not already in the list.
127 : *
128 : * @param node The device node to add
129 : * @param path The corresponding mount point path
130 : *
131 : * @return 0 on success, -1 in case of error.
132 : */
133 0 : int logstorage_store_dev_info(const char* node, const char* path)
134 : {
135 : struct LogstorageDeviceInfo* ptr = NULL;
136 : size_t path_len = 0;
137 :
138 0 : if ((node == NULL) || (path == NULL)) {
139 0 : pr_error("Invalid input\n");
140 0 : return -1;
141 : }
142 :
143 0 : if (logstorage_find_dev_info(node)) {
144 0 : pr_verbose("%s already in list.\n", node);
145 0 : print_list();
146 0 : return 0;
147 : }
148 :
149 0 : ptr = calloc(1, sizeof(struct LogstorageDeviceInfo));
150 :
151 0 : if (ptr == NULL) {
152 0 : pr_error("Node creation failed\n");
153 0 : return -1;
154 : }
155 :
156 0 : ptr->dev_node = strdup(node);
157 0 : path_len = strlen(path);
158 :
159 0 : if (path_len > DLT_MOUNT_PATH_MAX)
160 : path_len = (size_t)DLT_MOUNT_PATH_MAX;
161 :
162 0 : ptr->mnt_point = (char*)calloc(1, path_len + 1);
163 :
164 0 : if (ptr->mnt_point == NULL) {
165 0 : pr_error("memory allocation failed for mnt_point\n");
166 0 : free(ptr->dev_node);
167 0 : free(ptr);
168 : ptr = NULL;
169 0 : return -1;
170 : }
171 :
172 0 : ptr->mnt_point[path_len] = '\0';
173 0 : memcpy(ptr->mnt_point, path, path_len);
174 :
175 : /* Put it on head */
176 0 : ptr->next = g_info;
177 :
178 0 : if (g_info)
179 0 : g_info->prev = ptr;
180 :
181 0 : g_info = ptr;
182 :
183 0 : pr_verbose("%s added to list.\n", node);
184 0 : print_list();
185 :
186 0 : return 0;
187 : }
188 :
189 : /** @brief Remove a device from the list
190 : *
191 : * If the device is removed from the list, the mount point
192 : * pointer is given back to the caller. That means that
193 : * he has to free it.
194 : *
195 : * @param node The device node to be removed
196 : *
197 : * @return the mount point if the node is found, NULL either.
198 : */
199 0 : char* logstorage_delete_dev_info(const char* node)
200 : {
201 : struct LogstorageDeviceInfo* del = NULL;
202 : char* ret = NULL;
203 :
204 0 : del = logstorage_find_dev_info(node);
205 :
206 0 : if (del == NULL) {
207 0 : pr_verbose("%s not found in list.\n", node);
208 0 : print_list();
209 0 : return ret;
210 : }
211 :
212 : /* Has to be freed by the caller */
213 0 : ret = del->mnt_point;
214 :
215 0 : if (del->prev)
216 0 : del->prev->next = del->next;
217 :
218 0 : if (del->next)
219 0 : del->next->prev = del->prev;
220 :
221 0 : if (del == g_info)
222 0 : g_info = g_info->next;
223 :
224 0 : free(del->dev_node);
225 0 : free(del);
226 :
227 0 : pr_verbose("%s removed from list.\n", node);
228 0 : print_list();
229 :
230 0 : return ret;
231 : }
|