automotive-dlt
dlt-kpi-common.c
Go to the documentation of this file.
1 /*
2  * @licence app begin@
3  * SPDX license identifier: MPL-2.0
4  *
5  * Copyright (C) 2011-2015, BMW AG
6  *
7  * This file is part of GENIVI Project DLT - Diagnostic Log and Trace.
8  *
9  * This Source Code Form is subject to the terms of the
10  * Mozilla Public License (MPL), v. 2.0.
11  * If a copy of the MPL was not distributed with this file,
12  * You can obtain one at http://mozilla.org/MPL/2.0/.
13  *
14  * For further information see http://www.genivi.org/.
15  * @licence end@
16  */
17 
27 #include "dlt-kpi-common.h"
28 
29 static int dlt_kpi_cpu_count = -1;
30 
31 DltReturnValue dlt_kpi_read_file_compact(char *filename, char **target)
32 {
33  if(filename == NULL || target == NULL)
34  {
35  fprintf(stderr, "%s: Nullpointer parameter!\n",__func__);
37  }
38 
39  char buffer[BUFFER_SIZE];
40  int ret = dlt_kpi_read_file(filename, buffer, BUFFER_SIZE);
41  if(ret < DLT_RETURN_OK)
42  return ret;
43 
44  if((*target = malloc(strlen(buffer) + 1)) == NULL)
45  {
46  fprintf(stderr, "Out of memory!\n");
47  return DLT_RETURN_ERROR;
48  }
49 
50  memcpy(*target, buffer, strlen(buffer) + 1);
51 
52  return DLT_RETURN_OK;
53 }
54 
55 DltReturnValue dlt_kpi_read_file(char* filename, char* buffer, uint maxLength)
56 {
57  if(filename == NULL || buffer == NULL)
58  {
59  fprintf(stderr, "%s: Nullpointer parameter!\n",__func__);
61  }
62 
63  FILE* file = fopen(filename, "r");
64  if(file == NULL)
65  {
66  // fprintf(stderr, "Could not read file %s\n", filename);
67  return DLT_RETURN_ERROR;
68  }
69 
70  int buflen = fread(buffer, 1, maxLength-1, file);
71  buffer[buflen] = '\0';
72 
73  fclose(file);
74 
75  return DLT_RETURN_OK;
76 }
77 
79 {
80  char buffer[BUFFER_SIZE];
81  int ret = dlt_kpi_read_file("/proc/cpuinfo", buffer, sizeof(buffer));
82  if(ret != 0)
83  {
84  fprintf(stderr, "dlt_kpi_get_cpu_count(): Could not read /proc/cpuinfo\n");
85  return -1;
86  }
87 
88  char* delim = "[] \t\n";
89  char* tok = strtok(buffer, delim);
90  if(tok == NULL)
91  {
92  fprintf(stderr, "dlt_kpi_get_cpu_count(): Could not extract token\n");
93  return -1;
94  }
95 
96  int num = 0;
97  do
98  {
99  if(strcmp(tok, "processor") == 0)
100  num++;
101 
102  tok = strtok(NULL, delim);
103  }
104  while(tok != NULL);
105 
106  return num;
107 }
108 
110 {
111  if(dlt_kpi_cpu_count <= 0)
112  {
114  if(dlt_kpi_cpu_count <= 0)
115  {
116  fprintf(stderr, "Could not get CPU count\n");
117  dlt_kpi_cpu_count = -1;
118  }
119  }
120 
121  return dlt_kpi_cpu_count;
122 }
DltReturnValue
Definition: dlt_types.h:86
DltReturnValue dlt_kpi_read_file_compact(char *filename, char **target)
static int dlt_kpi_cpu_count
unsigned char buffer[BUFFER_SIZE]
Buffer for dlt file transfer. The size is defined by BUFFER_SIZE.
#define BUFFER_SIZE
int dlt_kpi_get_cpu_count()
int dlt_kpi_read_cpu_count()
DltReturnValue dlt_kpi_read_file(char *filename, char *buffer, uint maxLength)
#define NULL
Definition: dlt_common.h:232