From cf69741c24caa6639cfdaca9e8a07a68dc47501e Mon Sep 17 00:00:00 2001 From: Michael Kuc Date: Sun, 12 Aug 2018 11:33:41 +0100 Subject: [PATCH] Reused ACT (led0) light as activity light. --- Controller.cpp | 8 ++++++++ Controller.hpp | 6 ++++-- LED.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ LED.hpp | 6 ++++++ monitor.cpp | 23 ++++++++++++++++++++++- 5 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 LED.cpp create mode 100644 LED.hpp diff --git a/Controller.cpp b/Controller.cpp index 752f0ba..6e0f481 100644 --- a/Controller.cpp +++ b/Controller.cpp @@ -2,6 +2,7 @@ #include "u2f.hpp" #include #include "IO.hpp" +#include "LED.hpp" using namespace std; @@ -11,10 +12,17 @@ Controller::Controller(const uint32_t startChannel) void Controller::handleTransaction() { + if (channels.size() != 0 && chrono::duration_cast(chrono::system_clock::now() - lastMessage) < 5s) + toggleACTLED(); + else + enableACTLED(false); + auto msg = U2FMessage::readNonBlock(); if (!msg) return; + + lastMessage = chrono::system_clock::now(); auto opChannel = msg->cid; diff --git a/Controller.hpp b/Controller.hpp index b84be12..29dda50 100644 --- a/Controller.hpp +++ b/Controller.hpp @@ -1,12 +1,14 @@ #pragma once #include +#include #include "Channel.hpp" class Controller { protected: - std::map channels; - uint32_t currChannel; + std::map channels; + uint32_t currChannel; + std::chrono::system_clock::time_point lastMessage; public: Controller(const uint32_t startChannel = 1); diff --git a/LED.cpp b/LED.cpp new file mode 100644 index 0000000..949d650 --- /dev/null +++ b/LED.cpp @@ -0,0 +1,49 @@ +#include "LED.hpp" +#include +#include +#include + +using namespace std; + +bool& ledState() +{ + static bool state = true; + return state; +} + +bool getLEDState() +{ + return ledState(); +} + +void disableACTTrigger(bool nowDisabled) +{ + ofstream trigFile{ "/sys/class/leds/led0/trigger", ofstream::out | ofstream::trunc }; + + if (!trigFile) + throw runtime_error{ "Failed to retrieve led0 trigger file" }; + + if (!static_cast(trigFile << (nowDisabled ? "none" : "mmc0"))) + throw runtime_error{ "Failed to write led0 trigger to file" }; +} + +void enableACTLED(bool nowOn) +{ + if (nowOn == getLEDState()) + return; + + ofstream enabledFile{ "/sys/class/leds/led0/brightness", ofstream::out | ofstream::trunc }; + + if (!enabledFile) + throw runtime_error{ "Failed to retrieve led0 brightness" }; + + if (!static_cast(enabledFile << to_string(nowOn ? 0 : 1))) + throw runtime_error{ "Failed to write led0 brightness to file" }; + + ledState() = nowOn; +} + +void toggleACTLED() +{ + enableACTLED(!getLEDState()); +} diff --git a/LED.hpp b/LED.hpp new file mode 100644 index 0000000..c79d4f0 --- /dev/null +++ b/LED.hpp @@ -0,0 +1,6 @@ +#pragma once + +bool getLEDState(); +void disableACTTrigger(bool nowDisabled); +void enableACTLED(bool nowOn); +void toggleACTLED(); diff --git a/monitor.cpp b/monitor.cpp index 3a390ae..e2bd1cf 100644 --- a/monitor.cpp +++ b/monitor.cpp @@ -1,6 +1,7 @@ #include #include "Storage.hpp" #include "Controller.hpp" +#include "LED.hpp" #include #include @@ -12,6 +13,16 @@ volatile bool contProc = true; int main() { + try + { + disableACTTrigger(true); + enableACTLED(false); + } + catch (runtime_error &e) + { + cerr << e.what() << endl; + } + signal(SIGINT, signalCallback); Storage::init(); @@ -25,11 +36,21 @@ int main() Storage::save(); + try + { + disableACTTrigger(false); + enableACTLED(true); + } + catch (runtime_error &e) + { + cerr << e.what() << endl; + } + return EXIT_SUCCESS; } void signalCallback([[maybe_unused]] int signum) { contProc = false; - clog << "Caught SIGINT signal" << endl; + clog << "\nCaught SIGINT signal" << endl; }