automotive-dlt
dlt-logstorage-list.c
Go to the documentation of this file.
1 
25 /*******************************************************************************
26 ** **
27 ** SRC-MODULE: dlt-logstorage-list.c **
28 ** **
29 ** TARGET : linux **
30 ** **
31 ** PROJECT : DLT **
32 ** **
33 ** AUTHOR : Christoph Lipka clipka@jp.adit-jv.com **
34 ** Anitha.B.A anithaammaji.baggam@in.bosch.com **
35 ** Frederic Berat fberat@de.adit-jv.com **
36 ** **
37 ** PURPOSE : linked list implementation for storing the device info **
38 ** **
39 ** REMARKS : **
40 ** **
41 ** PLATFORM DEPENDANT [yes/no]: yes **
42 ** **
43 ** TO BE CHANGED BY USER [yes/no]: no **
44 ** **
45 *******************************************************************************/
46 
47 /*******************************************************************************
48 ** Author Identity **
49 ********************************************************************************
50 ** **
51 ** Initials Name Company **
52 ** -------- ------------------------- ---------------------------------- **
53 ** BA Anitha ADIT **
54 ** cl Christoph Lipka ADIT **
55 ** fb Frederic Berat ADIT **
56 *******************************************************************************/
57 #define pr_fmt(fmt) "Log storage list: "fmt
58 
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62 #include <unistd.h>
63 
64 #include "dlt_common.h"
65 #include "dlt-control-common.h"
66 #include "dlt-logstorage-common.h"
67 
68 static struct LogstorageDeviceInfo
69 {
70  char *dev_node;
71  char *mnt_point;
74 } *g_info;
75 
81 void print_list()
82 {
83  struct LogstorageDeviceInfo *ptr = g_info;
84  pr_verbose(" -------Device list-------\n");
85 
86  while (ptr != NULL)
87  {
88  pr_verbose("%p:\t[%s][%s] \n", ptr, ptr->dev_node, ptr->mnt_point);
89  ptr = ptr->next;
90  }
91 
92  pr_verbose(" -------Device list end-------\n\n");
93 
94  return;
95 }
96 
106 static struct LogstorageDeviceInfo *logstorage_find_dev_info(const char *node)
107 {
108  struct LogstorageDeviceInfo *ptr = g_info;
109 
110  if (!node)
111  {
112  return NULL;
113  }
114 
115  pr_verbose("Looking for %s.\n", node);
116 
117  while (ptr != NULL)
118  {
119  if (strncmp(ptr->dev_node, node, DLT_MOUNT_PATH_MAX) == 0)
120  {
121  pr_verbose("%s found in %p.\n", node, ptr);
122  break;
123  }
124  else
125  {
126  ptr = ptr->next;
127  }
128  }
129 
130  return ptr;
131 }
132 
143 int logstorage_store_dev_info(const char *node, const char *path)
144 {
145  struct LogstorageDeviceInfo *ptr = NULL;
146 
147  if ((node == NULL) || (path == NULL))
148  {
149  pr_error("Invalid input\n");
150  return -1;
151  }
152 
153  if (logstorage_find_dev_info(node))
154  {
155  pr_verbose("%s already in list.\n", node);
156  print_list();
157  return 0;
158  }
159 
160  ptr = calloc(1, sizeof(struct LogstorageDeviceInfo));
161 
162  if (ptr == NULL)
163  {
164  pr_error("Node creation failed\n");
165  return -1;
166  }
167 
168  ptr->dev_node = strdup(node);
169  ptr->mnt_point = strndup(path, DLT_MOUNT_PATH_MAX);
170 
171  /* Put it on head */
172  ptr->next = g_info;
173 
174  if (g_info)
175  {
176  g_info->prev = ptr;
177  }
178 
179  g_info = ptr;
180 
181  pr_verbose("%s added to list.\n", node);
182  print_list();
183 
184  return 0;
185 }
186 
197 char *logstorage_delete_dev_info(const char *node)
198 {
199  struct LogstorageDeviceInfo *del = NULL;
200  char *ret = NULL;
201 
202  del = logstorage_find_dev_info(node);
203 
204  if (del == NULL)
205  {
206  pr_verbose("%s not found in list.\n", node);
207  print_list();
208  return ret;
209  }
210 
211  /* Has to be freed by the caller */
212  ret = del->mnt_point;
213 
214  if (del->prev)
215  {
216  del->prev->next = del->next;
217  }
218 
219  if (del->next)
220  {
221  del->next->prev = del->prev;
222  }
223 
224  if (del == g_info)
225  {
226  g_info = g_info->next;
227  }
228 
229  free(del->dev_node);
230  free(del);
231 
232  pr_verbose("%s removed from list.\n", node);
233  print_list();
234 
235  return ret;
236 }
char * logstorage_delete_dev_info(const char *node)
Remove a device from the list.
struct LogstorageDeviceInfo * next
struct LogstorageDeviceInfo * prev
void print_list()
Prints the device list in verbose mode.
#define DLT_MOUNT_PATH_MAX
Definition: dlt_common.h:332
static struct LogstorageDeviceInfo * g_info
int logstorage_store_dev_info(const char *node, const char *path)
Add new device in the list.
#define pr_error(fmt,...)
#define pr_verbose(fmt,...)
static struct LogstorageDeviceInfo * logstorage_find_dev_info(const char *node)
Find element in the list based on device node.
#define NULL
Definition: dlt_common.h:232