pwmfan.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * <one line to give the library's name and an idea of what it does.>
  3. * Copyright 2015 Malte Veerman maldela@halloarsch.de
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License or (at your option) version 3 or any later version
  9. * accepted by the membership of KDE e.V. (or its successor approved
  10. * by the membership of KDE e.V.), which shall act as a proxy
  11. * defined in Section 14 of version 3 of the license.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #include "pwmfan.h"
  23. #include "hwmon.h"
  24. #include "fancontrolaction.h"
  25. #include <QtCore/QTextStream>
  26. #include <QtCore/QTimer>
  27. #include <QtCore/QDir>
  28. #include <QtCore/QFile>
  29. #include <KConfigCore/KConfigGroup>
  30. #include <KConfigCore/KSharedConfig>
  31. #include <KAuth/KAuthExecuteJob>
  32. #include <KI18n/KLocalizedString>
  33. #define TEST_HWMON_NAME "test"
  34. #define MAX_ERRORS_FOR_RPM_ZERO 10
  35. namespace Fancontrol
  36. {
  37. PwmFan::PwmFan(uint index, Hwmon *parent) : Fan(index, parent),
  38. m_pwmStream(new QTextStream),
  39. m_modeStream(new QTextStream),
  40. m_pwm(0),
  41. m_pwmMode(0),
  42. m_temp(Q_NULLPTR),
  43. m_hasTemp(false),
  44. m_minTemp(0),
  45. m_maxTemp(100),
  46. m_minPwm(255),
  47. m_maxPwm(255),
  48. m_minStart(255),
  49. m_minStop(255),
  50. m_zeroRpm(0),
  51. m_testStatus(NotStarted)
  52. {
  53. if (parent)
  54. {
  55. connect(this, &PwmFan::tempChanged, parent, &Hwmon::configUpdateNeeded);
  56. connect(this, &PwmFan::hasTempChanged, parent, &Hwmon::configUpdateNeeded);
  57. connect(this, &PwmFan::minTempChanged, parent, &Hwmon::configUpdateNeeded);
  58. connect(this, &PwmFan::maxTempChanged, parent, &Hwmon::configUpdateNeeded);
  59. connect(this, &PwmFan::minPwmChanged, parent, &Hwmon::configUpdateNeeded);
  60. connect(this, &PwmFan::maxPwmChanged, parent, &Hwmon::configUpdateNeeded);
  61. connect(this, &PwmFan::minStartChanged, parent, &Hwmon::configUpdateNeeded);
  62. connect(this, &PwmFan::minStopChanged, parent, &Hwmon::configUpdateNeeded);
  63. connect(this, &PwmFan::testStatusChanged, parent, &Hwmon::configUpdateNeeded);
  64. if (QDir(parent->path()).isReadable())
  65. {
  66. const auto pwmFile = new QFile(parent->path() + "/pwm" + QString::number(index), this);
  67. if (pwmFile->open(QFile::ReadWrite))
  68. {
  69. m_pwmStream->setDevice(pwmFile);
  70. *m_pwmStream >> m_pwm;
  71. }
  72. else if (pwmFile->open(QFile::ReadOnly))
  73. {
  74. m_pwmStream->setDevice(pwmFile);
  75. *m_pwmStream >> m_pwm;
  76. }
  77. else
  78. {
  79. emit error(i18n("Can't open pwmFile: %1", pwmFile->fileName()));
  80. delete pwmFile;
  81. }
  82. const auto pwmModeFile = new QFile(parent->path() + "/pwm" + QString::number(index) + "_mode", this);
  83. if (pwmModeFile->open(QFile::ReadWrite))
  84. {
  85. m_modeStream->setDevice(pwmModeFile);
  86. *m_modeStream >> m_pwmMode;
  87. }
  88. else if (pwmModeFile->open(QFile::ReadOnly))
  89. {
  90. m_modeStream->setDevice(pwmModeFile);
  91. *m_modeStream >> m_pwmMode;
  92. }
  93. else
  94. {
  95. emit error(i18n("Can't open pwmModeFile: %1", pwmModeFile->fileName()));
  96. delete pwmModeFile;
  97. }
  98. }
  99. }
  100. }
  101. PwmFan::~PwmFan()
  102. {
  103. auto device = m_pwmStream->device();
  104. delete m_pwmStream;
  105. delete device;
  106. device = m_modeStream->device();
  107. delete m_modeStream;
  108. delete device;
  109. }
  110. void PwmFan::update()
  111. {
  112. Fan::update();
  113. m_pwmStream->seek(0);
  114. setPwm(m_pwmStream->readAll().toInt(), false);
  115. m_modeStream->seek(0);
  116. setPwmMode(m_modeStream->readAll().toInt(), false);
  117. }
  118. void PwmFan::reset()
  119. {
  120. Fan::reset();
  121. setHasTemp(false);
  122. setTemp(Q_NULLPTR);
  123. setPwm(0);
  124. setPwmMode(0, true);
  125. setMinTemp(0);
  126. setMaxTemp(100);
  127. setMinPwm(255);
  128. setMaxPwm(255);
  129. setMinStart(255);
  130. setMinStop(255);
  131. m_zeroRpm = 0;
  132. m_testStatus = NotStarted;
  133. emit testStatusChanged();
  134. if (m_pwmStream->device() && m_modeStream->device() && m_parent)
  135. {
  136. auto device = m_pwmStream->device();
  137. m_pwmStream->setDevice(Q_NULLPTR);
  138. delete device;
  139. device = m_modeStream->device();
  140. m_modeStream->setDevice(Q_NULLPTR);
  141. delete device;
  142. const auto pwmFile = new QFile(m_parent->path() + "/pwm" + QString::number(m_index), this);
  143. if (pwmFile->open(QFile::ReadWrite))
  144. {
  145. m_pwmStream->setDevice(pwmFile);
  146. *m_pwmStream >> m_pwm;
  147. }
  148. else if (pwmFile->open(QFile::ReadOnly))
  149. {
  150. m_pwmStream->setDevice(pwmFile);
  151. *m_pwmStream >> m_pwm;
  152. }
  153. else
  154. {
  155. emit error(i18n("Can't open pwmFile: %1", pwmFile->fileName()));
  156. delete pwmFile;
  157. }
  158. const auto pwmModeFile = new QFile(m_parent->path() + "/pwm" + QString::number(m_index) + "_mode", this);
  159. if (pwmModeFile->open(QFile::ReadWrite))
  160. {
  161. m_modeStream->setDevice(pwmModeFile);
  162. *m_modeStream >> m_pwmMode;
  163. }
  164. else if (pwmModeFile->open(QFile::ReadOnly))
  165. {
  166. m_modeStream->setDevice(pwmModeFile);
  167. *m_modeStream >> m_pwmMode;
  168. }
  169. else
  170. {
  171. emit error(i18n("Can't open pwmModeFile: %1", pwmModeFile->fileName()));
  172. delete pwmModeFile;
  173. }
  174. }
  175. }
  176. bool PwmFan::isValid() const
  177. {
  178. return Fan::isValid() && (m_pwmStream->device() || m_pwmStream->string()) && (m_modeStream->device() || m_modeStream->string());
  179. }
  180. bool PwmFan::setPwm(int pwm, bool write)
  181. {
  182. if (pwm < 0 || pwm > 255)
  183. {
  184. emit error(i18n("Pwm cannot exceed 0-255!"), true);
  185. return false;
  186. }
  187. if (m_pwm != pwm)
  188. {
  189. m_pwm = pwm;
  190. emit pwmChanged();
  191. if (write)
  192. {
  193. setPwmMode(1);
  194. if (m_pwmStream->string() || (m_pwmStream->device() && m_pwmStream->device()->isWritable()))
  195. *m_pwmStream << pwm;
  196. else
  197. {
  198. auto action = newFancontrolAction();
  199. if (action.isValid())
  200. {
  201. QVariantMap map;
  202. map[QStringLiteral("action")] = "write";
  203. map[QStringLiteral("filename")] = qobject_cast<QFile *>(m_pwmStream->device())->fileName();
  204. map[QStringLiteral("content")] = QString::number(pwm);
  205. action.setArguments(map);
  206. const auto job = action.execute();
  207. if (!job->exec())
  208. {
  209. if (job->error() == KAuth::ActionReply::HelperBusyError)
  210. {
  211. // qDebug() << "Helper busy...";
  212. QTimer::singleShot(50, this, [this] (){ setPwmMode(m_pwmMode); });
  213. }
  214. emit error(i18n("Could not set pwm: %1", job->errorText()));
  215. }
  216. update();
  217. }
  218. else
  219. emit error(i18n("Action not supported! Try running the application as root."), true);
  220. }
  221. }
  222. }
  223. return true;
  224. }
  225. bool PwmFan::setPwmMode(int pwmMode, bool write)
  226. {
  227. if (pwmMode < 0 || pwmMode > 2)
  228. {
  229. emit error(i18n("PwmMode cannot exceed 0-2!"), true);
  230. return false;
  231. }
  232. if (m_pwmMode != pwmMode)
  233. {
  234. m_pwmMode = pwmMode;
  235. emit pwmModeChanged();
  236. if (write)
  237. {
  238. if (m_modeStream->string() || (m_modeStream->device() && m_modeStream->device()->isWritable()))
  239. *m_modeStream << pwmMode;
  240. else
  241. {
  242. auto action = newFancontrolAction();
  243. if (action.isValid())
  244. {
  245. QVariantMap map;
  246. map[QStringLiteral("action")] = QVariant("write");
  247. map[QStringLiteral("filename")] = qobject_cast<QFile *>(m_modeStream->device())->fileName();
  248. map[QStringLiteral("content")] = QString::number(pwmMode);
  249. action.setArguments(map);
  250. const auto job = action.execute();
  251. if (!job->exec())
  252. {
  253. if (job->error() == KAuth::ActionReply::HelperBusyError)
  254. {
  255. // qDebug() << "Helper busy...";
  256. QTimer::singleShot(50, this, [this] (){ setPwmMode(m_pwmMode); });
  257. }
  258. emit error(i18n("Could not set pwm mode: %1", job->errorText()));
  259. }
  260. update();
  261. }
  262. else
  263. emit error(i18n("Action not supported! Try running the application as root."), true);
  264. }
  265. }
  266. }
  267. return true;
  268. }
  269. void PwmFan::setMinPwm(int minPwm)
  270. {
  271. if (minPwm < 0 || minPwm > 255)
  272. {
  273. emit error(i18n("MinPwm cannot exceed 0-255!"), true);
  274. return;
  275. }
  276. if (minPwm != m_minPwm)
  277. {
  278. m_minPwm = minPwm;
  279. emit minPwmChanged();
  280. }
  281. }
  282. void PwmFan::setMaxPwm(int maxPwm)
  283. {
  284. if (maxPwm < 0 || maxPwm > 255)
  285. {
  286. emit error(i18n("MaxPwm cannot exceed 0-255!"), true);
  287. return;
  288. }
  289. if (maxPwm != m_maxPwm)
  290. {
  291. m_maxPwm = maxPwm;
  292. emit maxPwmChanged();
  293. }
  294. }
  295. void PwmFan::test()
  296. {
  297. if ((!m_modeStream->device()->isWritable() && !m_modeStream->string()) ||
  298. (!m_pwmStream->device()->isWritable() && !m_pwmStream->string()))
  299. {
  300. auto action = newFancontrolAction();
  301. if (action.isValid())
  302. {
  303. const auto job = action.execute(KAuth::Action::AuthorizeOnlyMode);
  304. if (!job->exec())
  305. {
  306. emit error(i18n("Authorization error: %1", job->errorText()));
  307. m_testStatus = Error;
  308. emit testStatusChanged();
  309. return;
  310. }
  311. }
  312. else
  313. {
  314. emit error(i18n("Action not supported! Try running the application as root."), true);
  315. return;
  316. }
  317. }
  318. setPwm(255);
  319. m_testStatus = FindingStop1;
  320. emit testStatusChanged();
  321. QTimer::singleShot(500, this, &PwmFan::continueTest);
  322. // qDebug() << "Start testing...";
  323. }
  324. void PwmFan::abortTest()
  325. {
  326. if (m_testStatus >= FindingStop1 && m_testStatus <= FindingStart)
  327. {
  328. // qDebug() << "Abort testing";
  329. m_testStatus = Cancelled;
  330. emit testStatusChanged();
  331. setPwm(255);
  332. }
  333. }
  334. void PwmFan::continueTest()
  335. {
  336. if ((!m_modeStream->device()->isWritable() && !m_modeStream->string()) ||
  337. (!m_pwmStream->device()->isWritable() && !m_pwmStream->string()))
  338. {
  339. auto action = newFancontrolAction();
  340. if (action.status() != KAuth::Action::AuthorizedStatus)
  341. {
  342. m_testStatus = Error;
  343. emit testStatusChanged();
  344. return;
  345. }
  346. }
  347. update();
  348. switch (m_testStatus)
  349. {
  350. case FindingStop1:
  351. if (rpm() > 0)
  352. {
  353. setPwm(qMin(m_pwm * 0.95, m_pwm - 5.0));
  354. m_zeroRpm = 0;
  355. }
  356. else
  357. {
  358. if (m_zeroRpm < MAX_ERRORS_FOR_RPM_ZERO)
  359. {
  360. m_zeroRpm++;
  361. }
  362. else
  363. {
  364. m_testStatus = FindingStart;
  365. m_zeroRpm = 0;
  366. // qDebug() << "Start finding start value...";
  367. }
  368. }
  369. QTimer::singleShot(500, this, &PwmFan::continueTest);
  370. break;
  371. case FindingStart:
  372. if (rpm() == 0)
  373. if (m_pwm >= 255)
  374. {
  375. m_testStatus = Finished;
  376. emit testStatusChanged();
  377. m_zeroRpm = 0;
  378. setMinStop(255);
  379. setMinStart(255);
  380. break;
  381. }
  382. else
  383. setPwm(qMax(m_pwm + 2, 255));
  384. else
  385. {
  386. m_testStatus = FindingStop2;
  387. setMinStart(m_pwm);
  388. // qDebug() << "Start finding stop value...";
  389. }
  390. QTimer::singleShot(1000, this, &PwmFan::continueTest);
  391. break;
  392. case FindingStop2:
  393. if (rpm() > 0)
  394. {
  395. setPwm(m_pwm - 1);
  396. m_zeroRpm = 0;
  397. QTimer::singleShot(1000, this, &PwmFan::continueTest);
  398. }
  399. else
  400. {
  401. if (m_zeroRpm < MAX_ERRORS_FOR_RPM_ZERO)
  402. {
  403. m_zeroRpm++;
  404. QTimer::singleShot(500, this, &PwmFan::continueTest);
  405. }
  406. else
  407. {
  408. m_testStatus = Finished;
  409. emit testStatusChanged();
  410. m_zeroRpm = 0;
  411. setMinStop(m_pwm + 5);
  412. setPwm(255);
  413. // qDebug() << "Finished testing PwmFan" << m_index;
  414. }
  415. }
  416. break;
  417. default:
  418. break;
  419. }
  420. }
  421. bool PwmFan::testing() const
  422. {
  423. return m_testStatus == FindingStop1 || m_testStatus == FindingStop2 || m_testStatus == FindingStart;
  424. }
  425. bool PwmFan::active() const
  426. {
  427. const auto active = KSharedConfig::openConfig(QStringLiteral("fancontrol-gui"))->group("active");
  428. const auto localActive = active.group(m_parent ? m_parent->name() : QStringLiteral(TEST_HWMON_NAME));
  429. return localActive.readEntry("pwmfan" + QString::number(m_index), true);
  430. }
  431. void PwmFan::setActive(bool a)
  432. {
  433. const auto active = KSharedConfig::openConfig(QStringLiteral("fancontrol-gui"))->group("active");
  434. auto localActive = active.group(m_parent ? m_parent->name() : QStringLiteral(TEST_HWMON_NAME));
  435. if (a != localActive.readEntry("pwmfan" + QString::number(m_index), true))
  436. {
  437. localActive.writeEntry("pwmfan" + QString::number(m_index), a);
  438. emit activeChanged();
  439. }
  440. }
  441. }