1414#include " file.hpp"
1515
1616#define FAILCNT_LIMIT 5
17- #define FILEBUF_SIZE 16
17+ #define FILEBUF_SIZE 32
1818
1919static struct Config {
2020 std::string sCPUTemp ;
@@ -37,6 +37,58 @@ static struct Config {
3737
3838static bool s_bRunning = true ;
3939
40+ std::string format (const char * pcFmt, ...) {
41+ char vcBuf[1024 ];
42+
43+ va_list vaArgs;
44+ va_start (vaArgs, pcFmt);
45+
46+ int nLen = vsprintf (vcBuf, pcFmt, vaArgs);
47+
48+ return std::string (vcBuf, nLen);
49+ }
50+
51+ bool readString (File& rfFile, std::string& rsStr) {
52+ if (!rfFile.isValid ()) {
53+ return false ;
54+ }
55+
56+ ssize_t nSize;
57+ char vcBuf[FILEBUF_SIZE + 1 ];
58+
59+ if ((nSize = rfFile.read (vcBuf, FILEBUF_SIZE)) <= 0 ) {
60+ return false ;
61+ }
62+
63+ rsStr = std::string (vcBuf, nSize);
64+ return true ;
65+ }
66+
67+ bool searchCPUHwmon () {
68+ int nIndex = 0 ;
69+
70+ while (true ) {
71+ std::string sPath , sName ;
72+ sPath = format (" /sys/class/hwmon/hwmon%d/" , nIndex);
73+
74+ File fName (sPath + " name" );
75+
76+ if (!fName .isValid () || !readString (fName , sName )) {
77+ break ;
78+ }
79+
80+ if (sName .compare (0 , 7 , " k10temp" ) == 0 ||
81+ sName .compare (0 , 8 , " coretemp" ) == 0 ) {
82+ s_cCfg.sCPUTemp = sPath + " temp1_input" ;
83+ return true ;
84+ }
85+
86+ nIndex++;
87+ }
88+
89+ return false ;
90+ }
91+
4092bool readUint (File& rfFile, unsigned & rnValue) {
4193 if (!rfFile.isValid ()) {
4294 return false ;
@@ -129,6 +181,15 @@ int main() {
129181 return ENOENT;
130182 }
131183
184+ if (s_cCfg.sCPUTemp .empty ()) {
185+ if (!searchCPUHwmon ()) {
186+ fprintf (stderr, " Cannot find CPU hwmon!\n " );
187+ return ENODEV;
188+ }
189+
190+ printf (" Found CPU hwmon: %s\n " , s_cCfg.sCPUTemp .c_str ());
191+ }
192+
132193 unsigned nCurrentTemp;
133194 unsigned nCurrentMode;
134195 int nFailCounter = 0 ;
0 commit comments