pwmfan.cpp 11 KB

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