pwmfan.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * <one line to give the library's name and an idea of what it does.>
  3. * Copyright 2015 Malte Veerman <malte.veerman@gmail.com>
  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_enableStream(new QTextStream),
  40. m_pwm(0),
  41. m_pwmEnable(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. setPwm(m_pwmStream->readAll().toInt(), false);
  71. }
  72. else if (pwmFile->open(QFile::ReadOnly))
  73. {
  74. m_pwmStream->setDevice(pwmFile);
  75. setPwm(m_pwmStream->readAll().toInt(), false);
  76. }
  77. else
  78. {
  79. emit error(i18n("Can't open pwm file: \'%1\'", pwmFile->fileName()));
  80. delete pwmFile;
  81. }
  82. const auto pwmEnableFile = new QFile(parent->path() + "/pwm" + QString::number(index) + "_enable", this);
  83. if (pwmEnableFile->open(QFile::ReadWrite))
  84. {
  85. m_enableStream->setDevice(pwmEnableFile);
  86. setPwmEnable(m_enableStream->readAll().toInt(), false);
  87. }
  88. else if (pwmEnableFile->open(QFile::ReadOnly))
  89. {
  90. m_enableStream->setDevice(pwmEnableFile);
  91. setPwmEnable(m_enableStream->readAll().toInt(), false);
  92. }
  93. else
  94. {
  95. emit error(i18n("Can't open pwm_enable file: \'%1\'", pwmEnableFile->fileName()));
  96. delete pwmEnableFile;
  97. }
  98. }
  99. }
  100. }
  101. PwmFan::~PwmFan()
  102. {
  103. auto device = m_pwmStream->device();
  104. delete m_pwmStream;
  105. delete device;
  106. device = m_enableStream->device();
  107. delete m_enableStream;
  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_enableStream->seek(0);
  116. setPwmEnable(m_enableStream->readAll().toInt(), false);
  117. }
  118. void PwmFan::toDefault()
  119. {
  120. Fan::toDefault();
  121. setHasTemp(false);
  122. setTemp(Q_NULLPTR);
  123. setPwm(0, false);
  124. setPwmEnable(0, false);
  125. setMinTemp(0);
  126. setMaxTemp(100);
  127. setMinPwm(255);
  128. setMaxPwm(255);
  129. setMinStart(255);
  130. setMinStop(255);
  131. m_zeroRpm = 0;
  132. if (m_testStatus != NotStarted)
  133. {
  134. m_testStatus = NotStarted;
  135. emit testStatusChanged();
  136. }
  137. if (m_pwmStream->device() && m_enableStream->device() && m_parent)
  138. {
  139. auto device = m_pwmStream->device();
  140. m_pwmStream->setDevice(Q_NULLPTR);
  141. delete device;
  142. device = m_enableStream->device();
  143. m_enableStream->setDevice(Q_NULLPTR);
  144. delete device;
  145. const auto pwmFile = new QFile(m_parent->path() + "/pwm" + QString::number(m_index), this);
  146. if (pwmFile->open(QFile::ReadWrite))
  147. {
  148. m_pwmStream->setDevice(pwmFile);
  149. *m_pwmStream >> m_pwm;
  150. }
  151. else if (pwmFile->open(QFile::ReadOnly))
  152. {
  153. m_pwmStream->setDevice(pwmFile);
  154. *m_pwmStream >> m_pwm;
  155. }
  156. else
  157. {
  158. emit error(i18n("Can't open pwm file: \'%1\'", pwmFile->fileName()));
  159. delete pwmFile;
  160. }
  161. const auto pwmEnableFile = new QFile(m_parent->path() + "/pwm" + QString::number(m_index) + "_enable", this);
  162. if (pwmEnableFile->open(QFile::ReadWrite))
  163. {
  164. m_enableStream->setDevice(pwmEnableFile);
  165. *m_enableStream >> m_pwmEnable;
  166. }
  167. else if (pwmEnableFile->open(QFile::ReadOnly))
  168. {
  169. m_enableStream->setDevice(pwmEnableFile);
  170. *m_enableStream >> m_pwmEnable;
  171. }
  172. else
  173. {
  174. emit error(i18n("Can't open pwm_enable file: \'%1\'", pwmEnableFile->fileName()));
  175. delete pwmEnableFile;
  176. }
  177. }
  178. }
  179. bool PwmFan::isValid() const
  180. {
  181. return Fan::isValid() && (m_pwmStream->device() || m_pwmStream->string()) && (m_enableStream->device() || m_enableStream->string());
  182. }
  183. bool PwmFan::setPwm(int pwm, bool write)
  184. {
  185. if (pwm < 0 || pwm > 255)
  186. {
  187. emit error(i18n("Pwm cannot exceed 0-255!"), true);
  188. return false;
  189. }
  190. if (m_pwm != pwm)
  191. {
  192. m_pwm = pwm;
  193. emit pwmChanged();
  194. if (write)
  195. {
  196. setPwmEnable(1, true);
  197. if (m_pwmStream->string() || (m_pwmStream->device() && m_pwmStream->device()->isWritable()))
  198. *m_pwmStream << pwm;
  199. else
  200. {
  201. auto action = newFancontrolAction();
  202. if (action.isValid())
  203. {
  204. QVariantMap map;
  205. map[QStringLiteral("action")] = "write";
  206. map[QStringLiteral("filename")] = qobject_cast<QFile *>(m_pwmStream->device())->fileName();
  207. map[QStringLiteral("content")] = QString::number(pwm);
  208. action.setArguments(map);
  209. const auto job = action.execute();
  210. if (!job->exec())
  211. {
  212. if (job->error() == KAuth::ActionReply::HelperBusyError)
  213. {
  214. // qDebug() << "Helper busy...";
  215. QTimer::singleShot(50, this, [this] (){ setPwmEnable(m_pwmEnable); });
  216. }
  217. emit error(i18n("Could not set pwm: %1", job->errorText()));
  218. }
  219. update();
  220. }
  221. else
  222. emit error(i18n("Action not supported! Try running the application as root."), true);
  223. }
  224. }
  225. }
  226. return true;
  227. }
  228. bool PwmFan::setPwmEnable(int pwmEnable, bool write)
  229. {
  230. if (pwmEnable < 0)
  231. {
  232. emit error(i18n("PwmEnable cannot be less than 0!"), true);
  233. return false;
  234. }
  235. if (m_pwmEnable != pwmEnable)
  236. {
  237. m_pwmEnable = pwmEnable;
  238. emit pwmEnableChanged();
  239. if (write)
  240. {
  241. if (m_enableStream->string() || (m_enableStream->device() && m_enableStream->device()->isWritable()))
  242. *m_enableStream << pwmEnable;
  243. else
  244. {
  245. auto action = newFancontrolAction();
  246. if (action.isValid())
  247. {
  248. QVariantMap map;
  249. map[QStringLiteral("action")] = QVariant("write");
  250. map[QStringLiteral("filename")] = qobject_cast<QFile *>(m_enableStream->device())->fileName();
  251. map[QStringLiteral("content")] = QString::number(pwmEnable);
  252. action.setArguments(map);
  253. const auto job = action.execute();
  254. if (!job->exec())
  255. {
  256. if (job->error() == KAuth::ActionReply::HelperBusyError)
  257. {
  258. // qDebug() << "Helper busy...";
  259. QTimer::singleShot(50, this, [this] (){ setPwmEnable(m_pwmEnable); });
  260. }
  261. emit error(i18n("Could not set pwm enable: %1", job->errorText()));
  262. }
  263. update();
  264. }
  265. else
  266. emit error(i18n("Action not supported! Try running the application as root."), true);
  267. }
  268. }
  269. }
  270. return true;
  271. }
  272. void PwmFan::setMinPwm(int minPwm)
  273. {
  274. if (minPwm < 0 || minPwm > 255)
  275. {
  276. emit error(i18n("MinPwm cannot exceed 0-255!"), true);
  277. return;
  278. }
  279. if (minPwm != m_minPwm)
  280. {
  281. m_minPwm = minPwm;
  282. emit minPwmChanged();
  283. }
  284. }
  285. void PwmFan::setMaxPwm(int maxPwm)
  286. {
  287. if (maxPwm < 0 || maxPwm > 255)
  288. {
  289. emit error(i18n("MaxPwm cannot exceed 0-255!"), true);
  290. return;
  291. }
  292. if (maxPwm != m_maxPwm)
  293. {
  294. m_maxPwm = maxPwm;
  295. emit maxPwmChanged();
  296. }
  297. }
  298. void PwmFan::test()
  299. {
  300. if ((!m_enableStream->device()->isWritable() && !m_enableStream->string()) ||
  301. (!m_pwmStream->device()->isWritable() && !m_pwmStream->string()))
  302. {
  303. auto action = newFancontrolAction();
  304. if (action.isValid())
  305. {
  306. const auto job = action.execute(KAuth::Action::AuthorizeOnlyMode);
  307. if (!job->exec())
  308. {
  309. emit error(i18n("Authorization error: %1", job->errorText()));
  310. m_testStatus = Error;
  311. emit testStatusChanged();
  312. return;
  313. }
  314. }
  315. else
  316. {
  317. emit error(i18n("Action not supported! Try running the application as root."), true);
  318. return;
  319. }
  320. }
  321. setPwm(255, true);
  322. m_testStatus = FindingStop1;
  323. emit testStatusChanged();
  324. QTimer::singleShot(500, this, &PwmFan::continueTest);
  325. // qDebug() << "Start testing...";
  326. }
  327. void PwmFan::abortTest()
  328. {
  329. if (m_testStatus >= FindingStop1 && m_testStatus <= FindingStart)
  330. {
  331. // qDebug() << "Abort testing";
  332. m_testStatus = Cancelled;
  333. emit testStatusChanged();
  334. setPwm(255);
  335. setPwmEnable(0);
  336. }
  337. }
  338. void PwmFan::continueTest()
  339. {
  340. if ((!m_enableStream->device()->isWritable() && !m_enableStream->string()) ||
  341. (!m_pwmStream->device()->isWritable() && !m_pwmStream->string()))
  342. {
  343. auto action = newFancontrolAction();
  344. if (action.status() != KAuth::Action::AuthorizedStatus)
  345. {
  346. m_testStatus = Error;
  347. emit testStatusChanged();
  348. return;
  349. }
  350. }
  351. update();
  352. switch (m_testStatus)
  353. {
  354. case FindingStop1:
  355. if (rpm() > 0)
  356. {
  357. if (m_pwm == 0)
  358. {
  359. error(i18n("Fan never stops."), false);
  360. setMinStart(0);
  361. setMinStop(0);
  362. setMinPwm(0);
  363. setPwm(255);
  364. m_testStatus = Finished;
  365. emit testStatusChanged();
  366. return;
  367. }
  368. setPwm(qMax(0, (int)qMin(m_pwm * 0.95, m_pwm - 5.0)));
  369. m_zeroRpm = 0;
  370. }
  371. else
  372. {
  373. if (m_zeroRpm < MAX_ERRORS_FOR_RPM_ZERO)
  374. {
  375. m_zeroRpm++;
  376. }
  377. else
  378. {
  379. m_testStatus = FindingStart;
  380. m_zeroRpm = 0;
  381. // qDebug() << "Start finding start value...";
  382. }
  383. }
  384. QTimer::singleShot(500, this, &PwmFan::continueTest);
  385. break;
  386. case FindingStart:
  387. if (rpm() == 0)
  388. if (m_pwm >= 255)
  389. {
  390. m_testStatus = Finished;
  391. emit testStatusChanged();
  392. m_zeroRpm = 0;
  393. setMinStop(255);
  394. setMinStart(255);
  395. break;
  396. }
  397. else
  398. setPwm(qMin(m_pwm + 2, 255));
  399. else
  400. {
  401. m_testStatus = FindingStop2;
  402. setMinStart(m_pwm);
  403. // qDebug() << "Start finding stop value...";
  404. }
  405. QTimer::singleShot(1000, this, &PwmFan::continueTest);
  406. break;
  407. case FindingStop2:
  408. if (rpm() > 0)
  409. {
  410. setPwm(qMax(0, m_pwm - 1));
  411. m_zeroRpm = 0;
  412. QTimer::singleShot(1000, this, &PwmFan::continueTest);
  413. }
  414. else
  415. {
  416. if (m_zeroRpm < MAX_ERRORS_FOR_RPM_ZERO)
  417. {
  418. m_zeroRpm++;
  419. QTimer::singleShot(500, this, &PwmFan::continueTest);
  420. }
  421. else
  422. {
  423. m_testStatus = Finished;
  424. emit testStatusChanged();
  425. m_zeroRpm = 0;
  426. setMinStop(qMin(255, m_pwm + 5));
  427. setMinPwm(qMin(m_minPwm, m_minStop));
  428. setPwm(255);
  429. // qDebug() << "Finished testing PwmFan" << m_index;
  430. }
  431. }
  432. break;
  433. default:
  434. break;
  435. }
  436. }
  437. bool PwmFan::testing() const
  438. {
  439. return m_testStatus == FindingStop1 || m_testStatus == FindingStop2 || m_testStatus == FindingStart;
  440. }
  441. bool PwmFan::active() const
  442. {
  443. const auto active = KSharedConfig::openConfig(QStringLiteral("fancontrol-gui"))->group("active");
  444. const auto localActive = active.group(m_parent ? m_parent->name() : QStringLiteral(TEST_HWMON_NAME));
  445. return localActive.readEntry("pwmfan" + QString::number(m_index), true);
  446. }
  447. void PwmFan::setActive(bool a)
  448. {
  449. const auto active = KSharedConfig::openConfig(QStringLiteral("fancontrol-gui"))->group("active");
  450. auto localActive = active.group(m_parent ? m_parent->name() : QStringLiteral(TEST_HWMON_NAME));
  451. if (a != localActive.readEntry("pwmfan" + QString::number(m_index), true))
  452. {
  453. localActive.writeEntry("pwmfan" + QString::number(m_index), a);
  454. emit activeChanged();
  455. }
  456. }
  457. }