1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
/* $Id$
* $URL$
*
* plugin raspi
*
* Copyright (C) 2003 Michael Reinelt <michael@reinelt.co.at>
* Copyright (C) 2013 Volker Gerng <v.gering@t-online.de>
* Copyright (C) 2004, 2005, 2006, 2007, 2008 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
*
* This file is part of LCD4Linux.
*
* LCD4Linux is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* LCD4Linux is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
/*
* exported functions:
*
* int plugin_init_raspi (void)
* adds functions to get information about internal sensors of raspberry pi
*
*/
#include "config.h"
/* these should always be included */
#include "debug.h"
#include "plugin.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#ifdef WITH_DMALLOC
#include <dmalloc.h>
#endif
#define RASPI_FREQ_PATH "/sys/devices/system/cpu/cpu0/cpufreq/"
#define RASPI_FREQ_VALUE "cpuinfo_cur_freq"
#define RASPI_FREQ_IDFILE "scaling_driver"
#define RASPI_TEMP_PATH "/sys/class/thermal/thermal_zone0/"
#define RASPI_TEMP_VALUE "temp"
#define RASPI_TEMP_IDFILE "type"
#define RASPI_TEMP_ID "bcm2835_thermal"
#define _cat(a,b) (a##b)
#define strings(a, b) "_cat(a,b)"
char tmpstr[128];
/* Note: all local functions should be declared 'static' */
/* reading an positive integer value from path, -1 on error */
static int readValue(char *path) {
int value = -1;
FILE *fp;
fp = fopen(path, "r");
if (NULL != fp) {
fgets(tmpstr, sizeof(tmpstr), fp);
fclose(fp);
if (1 != sscanf(tmpstr, "%i", &value)) {
error("[raspi] error reading integer value from %s\n", path);
}
} else {
error("[raspi] error opening %s: %s\n", path, strerror(errno));
}
return value;
}
/* reads a string from path */
static char* readStr(char *path) {
FILE *fp;
memset(tmpstr, 0, sizeof(tmpstr));
fp = fopen(path, "r");
if (NULL != fp) {
fgets(tmpstr, sizeof(tmpstr), fp);
fclose(fp);
} else {
error("[raspi] error reading text value from %s: %s\n", path, strerror(errno));
}
return tmpstr;
}
/* reads the actual cpu frequency of the bcm2708 cpu */
static void my_cpufreq(RESULT * result)
{
snprintf(tmpstr, sizeof(tmpstr), "%s%s", RASPI_FREQ_PATH, RASPI_FREQ_VALUE);
double value = readValue(tmpstr) / 1000.0L;
info("[raspi] actual cpu frequency: %.2f MHz", value);
SetResult(&result, R_NUMBER, &value);
}
/* reads the actual cpu temperature in degree Celsius */
static void my_cputemp(RESULT * result)
{
snprintf(tmpstr, sizeof(tmpstr), "%s%s", RASPI_TEMP_PATH, RASPI_TEMP_VALUE);
double value = readValue(tmpstr) / 1000.0L;
info("[raspi] actual cpu temperature: %.1f C", value);
SetResult(&result, R_NUMBER, &value);
}
/* plugin initialization */
/* MUST NOT be declared 'static'! */
int plugin_init_raspi(void)
{
char checkFile[128];
AddFunction("raspi::cpufreq", 0, my_cpufreq);
AddFunction("raspi::cputemp", 0, my_cputemp);
snprintf(checkFile, sizeof(checkFile), "%s%s", RASPI_TEMP_PATH, RASPI_TEMP_IDFILE);
if (strncmp( readStr(checkFile), RASPI_TEMP_ID, strlen(RASPI_TEMP_ID) ) != 0) {
error("Warning: no raspberry pi thermal sensor found: value of '%s' is '%s', should be '%s'",
checkFile, readStr(checkFile), RASPI_TEMP_IDFILE);
}
return 0;
}
void plugin_exit_raspi(void)
{
/* free any allocated memory */
/* close filedescriptors */
}
|