pwmfan.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. qDebug() << "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. qDebug() << "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. qDebug() << "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. qDebug() << "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. qDebug() << "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 *reply = action.execute();
  161. if (!reply->exec())
  162. {
  163. qDebug() << "setPwm error:" << reply->errorString() << reply->errorText();
  164. return false;
  165. }
  166. }
  167. }
  168. }
  169. return true;
  170. }
  171. bool PwmFan::setPwmMode(int pwmMode, bool write)
  172. {
  173. if (m_pwmMode != pwmMode)
  174. {
  175. m_pwmMode = pwmMode;
  176. emit pwmModeChanged();
  177. if (write)
  178. {
  179. if (m_modeStream->device()->isWritable())
  180. *m_modeStream << pwmMode;
  181. else
  182. {
  183. KAuth::Action action("fancontrol.gui.helper.action");
  184. action.setHelperId("fancontrol.gui.helper");
  185. if (!action.isValid())
  186. {
  187. qDebug() << "setPwmMode action is invalid";
  188. return false;
  189. }
  190. QVariantMap map;
  191. map["action"] = "write";
  192. map["filename"] = qobject_cast<QFile *>(m_modeStream->device())->fileName();
  193. map["content"] = QString::number(pwmMode);
  194. action.setArguments(map);
  195. KAuth::ExecuteJob *reply = action.execute();
  196. if (!reply->exec())
  197. {
  198. qDebug() << "setPwmMode error:" << reply->errorString() << reply->errorText();
  199. return false;
  200. }
  201. }
  202. }
  203. }
  204. return true;
  205. }
  206. bool PwmFan::test()
  207. {
  208. if (setPwmMode(1) && setPwm(255))
  209. {
  210. m_testStatus = FindingStop1;
  211. emit testingChanged();
  212. QTimer::singleShot(500, this, SLOT(continueTest()));
  213. qDebug() << "Start testing...";
  214. }
  215. else
  216. {
  217. qDebug() << "Testing failed";
  218. return false;
  219. }
  220. return true;
  221. }
  222. void PwmFan::abortTest()
  223. {
  224. setPwm(255);
  225. disconnect(0, 0, this, SLOT(continueTest()));
  226. m_testStatus = Cancelled;
  227. emit testingChanged();
  228. setPwm(255);
  229. }
  230. void PwmFan::continueTest()
  231. {
  232. update();
  233. switch (m_testStatus)
  234. {
  235. case FindingStop1:
  236. if (m_rpm > 0)
  237. {
  238. setPwm(qMin(m_pwm * 0.95, m_pwm - 5.0));
  239. m_zeroRpm = 0;
  240. }
  241. else
  242. {
  243. if (m_zeroRpm < MAX_ERRORS_FOR_RPM_ZERO)
  244. {
  245. m_zeroRpm++;
  246. }
  247. else
  248. {
  249. m_testStatus = FindingStart;
  250. m_zeroRpm = 0;
  251. qDebug() << "Start finding start value...";
  252. }
  253. }
  254. QTimer::singleShot(500, this, SLOT(continueTest()));
  255. break;
  256. case FindingStart:
  257. if (m_rpm == 0)
  258. setPwm(m_pwm + 2);
  259. else
  260. {
  261. m_testStatus = FindingStop2;
  262. setMinStart(m_pwm);
  263. qDebug() << "Start finding stop value...";
  264. }
  265. QTimer::singleShot(1000, this, SLOT(continueTest()));
  266. break;
  267. case FindingStop2:
  268. if (m_rpm > 0)
  269. {
  270. setPwm(m_pwm - 1);
  271. m_zeroRpm = 0;
  272. QTimer::singleShot(1000, this, SLOT(continueTest()));
  273. }
  274. else
  275. {
  276. if (m_zeroRpm < MAX_ERRORS_FOR_RPM_ZERO)
  277. {
  278. m_zeroRpm++;
  279. QTimer::singleShot(500, this, SLOT(continueTest()));
  280. }
  281. else
  282. {
  283. m_testStatus = Finished;
  284. emit testingChanged();
  285. m_zeroRpm = 0;
  286. setMinStop(m_pwm + 5);
  287. qDebug() << "Finished testing!";
  288. }
  289. }
  290. break;
  291. default:
  292. break;
  293. }
  294. }
  295. bool PwmFan::testing() const
  296. {
  297. return m_testStatus == FindingStop1 || m_testStatus == FindingStop2 || m_testStatus == FindingStart;
  298. }
  299. bool PwmFan::active() const
  300. {
  301. KConfigGroup active = KSharedConfig::openConfig("fancontrol-gui")->group("active");
  302. KConfigGroup localActive = active.group(m_parent->name());
  303. return localActive.readEntry("pwmfan" + QString::number(m_index), true);
  304. }
  305. void PwmFan::setActive(bool a)
  306. {
  307. KConfigGroup active = KSharedConfig::openConfig("fancontrol-gui")->group("active");
  308. KConfigGroup localActive = active.group(m_parent->name());
  309. if (a != localActive.readEntry("pwmfan" + QString::number(m_index), true))
  310. {
  311. localActive.writeEntry("pwmfan" + QString::number(m_index), a);
  312. emit activeChanged();
  313. }
  314. }
  315. }