pwmfan.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 <QtCore/QDebug>
  30. #include <KConfigCore/KConfigGroup>
  31. #include <KConfigCore/KSharedConfig>
  32. #include <KAuth/KAuthExecuteJob>
  33. #include <KI18n/KLocalizedString>
  34. #define MAX_ERRORS_FOR_RPM_ZERO 10
  35. namespace Fancontrol
  36. {
  37. PwmFan::PwmFan(Hwmon *parent, uint index) : Fan(parent, index),
  38. m_pwmStream(new QTextStream),
  39. m_modeStream(new QTextStream),
  40. m_temp(Q_NULLPTR),
  41. m_hasTemp(false),
  42. m_minTemp(0),
  43. m_maxTemp(100),
  44. m_minPwm(255),
  45. m_maxPwm(255),
  46. m_minStart(255),
  47. m_minStop(255),
  48. m_zeroRpm(0),
  49. m_testStatus(NotStarted)
  50. {
  51. connect(this, &PwmFan::tempChanged, parent, &Hwmon::updateConfig);
  52. connect(this, &PwmFan::hasTempChanged, parent, &Hwmon::updateConfig);
  53. connect(this, &PwmFan::minTempChanged, parent, &Hwmon::updateConfig);
  54. connect(this, &PwmFan::maxTempChanged, parent, &Hwmon::updateConfig);
  55. connect(this, &PwmFan::minPwmChanged, parent, &Hwmon::updateConfig);
  56. connect(this, &PwmFan::maxPwmChanged, parent, &Hwmon::updateConfig);
  57. connect(this, &PwmFan::minStartChanged, parent, &Hwmon::updateConfig);
  58. connect(this, &PwmFan::minStopChanged, parent, &Hwmon::updateConfig);
  59. connect(this, &PwmFan::testStatusChanged, parent, &Hwmon::updateConfig);
  60. if (QDir(parent->path()).isReadable())
  61. {
  62. const auto pwmFile = new QFile(parent->path() + "/pwm" + QString::number(index), this);
  63. if (pwmFile->open(QFile::ReadWrite))
  64. {
  65. m_pwmStream->setDevice(pwmFile);
  66. *m_pwmStream >> m_pwm;
  67. }
  68. else if (pwmFile->open(QFile::ReadOnly))
  69. {
  70. m_pwmStream->setDevice(pwmFile);
  71. *m_pwmStream >> m_pwm;
  72. }
  73. else
  74. {
  75. qWarning() << "Can't open pwmFile " << pwmFile->fileName();
  76. delete pwmFile;
  77. }
  78. const auto pwmModeFile = new QFile(parent->path() + "/pwm" + QString::number(index) + "_mode", this);
  79. if (pwmModeFile->open(QFile::ReadWrite))
  80. {
  81. m_modeStream->setDevice(pwmModeFile);
  82. *m_modeStream >> m_pwmMode;
  83. }
  84. else if (pwmModeFile->open(QFile::ReadOnly))
  85. {
  86. m_modeStream->setDevice(pwmModeFile);
  87. *m_modeStream >> m_pwmMode;
  88. }
  89. else
  90. {
  91. qWarning() << "Can't open pwmModeFile " << pwmModeFile->fileName();
  92. delete pwmModeFile;
  93. }
  94. }
  95. }
  96. PwmFan::~PwmFan()
  97. {
  98. delete m_pwmStream;
  99. delete m_modeStream;
  100. }
  101. void PwmFan::update()
  102. {
  103. Fan::update();
  104. m_pwmStream->seek(0);
  105. setPwm(m_pwmStream->readAll().toInt(), false);
  106. m_modeStream->seek(0);
  107. setPwmMode(m_modeStream->readAll().toInt(), false);
  108. }
  109. void PwmFan::reset()
  110. {
  111. Fan::reset();
  112. setHasTemp(false);
  113. setTemp(Q_NULLPTR);
  114. delete m_pwmStream->device();
  115. delete m_pwmStream;
  116. delete m_modeStream->device();
  117. delete m_modeStream;
  118. const auto pwmFile = new QFile(m_parent->path() + "/pwm" + QString::number(m_index), this);
  119. if (pwmFile->open(QFile::ReadWrite))
  120. {
  121. m_pwmStream = new QTextStream(pwmFile);
  122. *m_pwmStream >> m_pwm;
  123. }
  124. else if (pwmFile->open(QFile::ReadOnly))
  125. {
  126. m_pwmStream = new QTextStream(pwmFile);
  127. *m_pwmStream >> m_pwm;
  128. }
  129. else
  130. {
  131. qWarning() << "Can't open pwmFile " << pwmFile->fileName();
  132. delete pwmFile;
  133. }
  134. const auto pwmModeFile = new QFile(m_parent->path() + "/pwm" + QString::number(m_index) + "_mode", this);
  135. if (pwmModeFile->open(QFile::ReadWrite))
  136. {
  137. m_modeStream = new QTextStream(pwmModeFile);
  138. *m_modeStream >> m_pwmMode;
  139. }
  140. else if (pwmModeFile->open(QFile::ReadOnly))
  141. {
  142. m_modeStream = new QTextStream(pwmModeFile);
  143. *m_modeStream >> m_pwmMode;
  144. }
  145. else
  146. {
  147. qWarning() << "Can't open pwmModeFile " << pwmModeFile->fileName();
  148. delete pwmModeFile;
  149. }
  150. }
  151. bool PwmFan::setPwm(int pwm, bool write)
  152. {
  153. if (m_pwm != pwm)
  154. {
  155. m_pwm = pwm;
  156. emit pwmChanged();
  157. if (write)
  158. {
  159. setPwmMode(1);
  160. if (m_pwmStream->device()->isWritable())
  161. *m_pwmStream << pwm;
  162. else
  163. {
  164. auto action = newFancontrolAction();
  165. if (action.isValid())
  166. {
  167. QVariantMap map;
  168. map[QStringLiteral("action")] = "write";
  169. map[QStringLiteral("filename")] = qobject_cast<QFile *>(m_pwmStream->device())->fileName();
  170. map[QStringLiteral("content")] = QString::number(pwm);
  171. action.setArguments(map);
  172. const auto job = action.execute();
  173. if (!job->exec())
  174. {
  175. if (job->error() == KAuth::ActionReply::HelperBusyError)
  176. {
  177. qDebug() << "Helper busy...";
  178. QTimer::singleShot(50, this, [this] (){ setPwmMode(m_pwmMode); });
  179. }
  180. emit errorChanged(i18n("Could not set pwm: ") + job->errorText());
  181. }
  182. update();
  183. }
  184. else
  185. emit errorChanged(i18n("Action not supported! Try running the application as root."), true);
  186. }
  187. }
  188. }
  189. return true;
  190. }
  191. bool PwmFan::setPwmMode(int pwmMode, bool write)
  192. {
  193. if (m_pwmMode != pwmMode)
  194. {
  195. m_pwmMode = pwmMode;
  196. emit pwmModeChanged();
  197. if (write)
  198. {
  199. if (m_modeStream->device()->isWritable())
  200. *m_modeStream << pwmMode;
  201. else
  202. {
  203. auto action = newFancontrolAction();
  204. if (action.isValid())
  205. {
  206. QVariantMap map;
  207. map[QStringLiteral("action")] = QVariant("write");
  208. map[QStringLiteral("filename")] = qobject_cast<QFile *>(m_modeStream->device())->fileName();
  209. map[QStringLiteral("content")] = QString::number(pwmMode);
  210. action.setArguments(map);
  211. const auto job = action.execute();
  212. if (!job->exec())
  213. {
  214. if (job->error() == KAuth::ActionReply::HelperBusyError)
  215. {
  216. qDebug() << "Helper busy...";
  217. QTimer::singleShot(50, this, [this] (){ setPwmMode(m_pwmMode); });
  218. }
  219. emit errorChanged(i18n("Could not set pwm mode: ") + job->errorText());
  220. }
  221. update();
  222. }
  223. else
  224. emit errorChanged(i18n("Action not supported! Try running the application as root."), true);
  225. }
  226. }
  227. }
  228. return true;
  229. }
  230. void PwmFan::test()
  231. {
  232. if (!m_modeStream->device()->isWritable() || !m_pwmStream->device()->isWritable())
  233. {
  234. auto action = newFancontrolAction();
  235. if (action.isValid())
  236. {
  237. const auto job = action.execute(KAuth::Action::AuthorizeOnlyMode);
  238. if (!job->exec())
  239. {
  240. emit errorChanged(i18n("Authorization error: ") + job->errorText());
  241. m_testStatus = Error;
  242. emit testStatusChanged();
  243. return;
  244. }
  245. }
  246. else
  247. {
  248. emit errorChanged(i18n("Action not supported! Try running the application as root."), true);
  249. return;
  250. }
  251. }
  252. setPwm(255, true);
  253. m_testStatus = FindingStop1;
  254. emit testStatusChanged();
  255. QTimer::singleShot(500, this, &PwmFan::continueTest);
  256. qDebug() << "Start testing...";
  257. }
  258. void PwmFan::abortTest()
  259. {
  260. if (m_testStatus >= FindingStop1 && m_testStatus <= FindingStart)
  261. {
  262. qDebug() << "Abort testing";
  263. m_testStatus = Cancelled;
  264. emit testStatusChanged();
  265. setPwm(255);
  266. }
  267. }
  268. void PwmFan::continueTest()
  269. {
  270. if (!m_modeStream->device()->isWritable() || !m_pwmStream->device()->isWritable())
  271. {
  272. auto action = newFancontrolAction();
  273. if (action.status() != KAuth::Action::AuthorizedStatus)
  274. {
  275. m_testStatus = Error;
  276. emit testStatusChanged();
  277. return;
  278. }
  279. }
  280. update();
  281. switch (m_testStatus)
  282. {
  283. case FindingStop1:
  284. if (m_rpm > 0)
  285. {
  286. setPwm(qMin(m_pwm * 0.95, m_pwm - 5.0));
  287. m_zeroRpm = 0;
  288. }
  289. else
  290. {
  291. if (m_zeroRpm < MAX_ERRORS_FOR_RPM_ZERO)
  292. {
  293. m_zeroRpm++;
  294. }
  295. else
  296. {
  297. m_testStatus = FindingStart;
  298. m_zeroRpm = 0;
  299. qDebug() << "Start finding start value...";
  300. }
  301. }
  302. QTimer::singleShot(500, this, &PwmFan::continueTest);
  303. break;
  304. case FindingStart:
  305. if (m_rpm == 0)
  306. setPwm(m_pwm + 2);
  307. else
  308. {
  309. m_testStatus = FindingStop2;
  310. setMinStart(m_pwm);
  311. qDebug() << "Start finding stop value...";
  312. }
  313. QTimer::singleShot(1000, this, &PwmFan::continueTest);
  314. break;
  315. case FindingStop2:
  316. if (m_rpm > 0)
  317. {
  318. setPwm(m_pwm - 1);
  319. m_zeroRpm = 0;
  320. QTimer::singleShot(1000, this, &PwmFan::continueTest);
  321. }
  322. else
  323. {
  324. if (m_zeroRpm < MAX_ERRORS_FOR_RPM_ZERO)
  325. {
  326. m_zeroRpm++;
  327. QTimer::singleShot(500, this, &PwmFan::continueTest);
  328. }
  329. else
  330. {
  331. m_testStatus = Finished;
  332. emit testStatusChanged();
  333. m_zeroRpm = 0;
  334. setMinStop(m_pwm + 5);
  335. setPwm(255);
  336. qDebug() << "Finished testing PwmFan" << m_index;
  337. }
  338. }
  339. break;
  340. default:
  341. break;
  342. }
  343. }
  344. bool PwmFan::testing() const
  345. {
  346. return m_testStatus == FindingStop1 || m_testStatus == FindingStop2 || m_testStatus == FindingStart;
  347. }
  348. bool PwmFan::active() const
  349. {
  350. const auto active = KSharedConfig::openConfig(QStringLiteral("fancontrol-gui"))->group("active");
  351. const auto localActive = active.group(m_parent->name());
  352. return localActive.readEntry("pwmfan" + QString::number(m_index), true);
  353. }
  354. void PwmFan::setActive(bool a)
  355. {
  356. const auto active = KSharedConfig::openConfig(QStringLiteral("fancontrol-gui"))->group("active");
  357. auto localActive = active.group(m_parent->name());
  358. if (a != localActive.readEntry("pwmfan" + QString::number(m_index), true))
  359. {
  360. localActive.writeEntry("pwmfan" + QString::number(m_index), a);
  361. emit activeChanged();
  362. }
  363. }
  364. }