diff --git a/Streams.cpp b/Streams.cpp index 077ba91..15cfbd7 100644 --- a/Streams.cpp +++ b/Streams.cpp @@ -36,7 +36,7 @@ using std::runtime_error; using std::shared_ptr; using std::string; -shared_ptr initialiseHostDescriptor(); +shared_ptr genHostDescriptor(); #ifdef DEBUG_STREAMS @@ -44,96 +44,112 @@ FILE* initHTML(FILE* fPtr, const string& title); void closeHTML(FILE* fPtr); +shared_ptr genComHostStream(); +shared_ptr genHostPacketStream(); +shared_ptr genHostAPDUStream(); +shared_ptr genComDevStream(); +shared_ptr genDevPacketStream(); +shared_ptr genDevAPDUStream(); + #endif -shared_ptr getHostDescriptor() { - static shared_ptr descriptor{ initialiseHostDescriptor() }; +shared_ptr& getHostDescriptor() { + static shared_ptr descriptor{ +#ifndef MANUAL_LIFETIME + genHostDescriptor() +#endif + }; return descriptor; } #ifdef DEBUG_STREAMS -shared_ptr getComHostStream() { - static shared_ptr stream{ fopen((cacheDirectory + "/comhost.txt").c_str(), "wb"), - [](FILE* f) { - clog << "Closing comhost stream" << endl; - fclose(f); - } }; - - if (!stream) - clog << "Stream is unavailable" << endl; - - return stream; -} - -shared_ptr getHostPacketStream() { +shared_ptr& getComHostStream() { static shared_ptr stream{ - initHTML(fopen((cacheDirectory + "/hostpackets.html").c_str(), "wb"), "Host Packets"), - [](FILE* f) { - clog << "Closing hostPackets stream" << endl; - closeHTML(f); - } +#ifndef MANUAL_LIFETIME + genComHostStream() +#endif }; +#ifndef MANUAL_LIFETIME if (!stream) clog << "Stream is unavailable" << endl; +#endif return stream; } -shared_ptr getHostAPDUStream() { +shared_ptr& getHostPacketStream() { static shared_ptr stream{ - initHTML(fopen((cacheDirectory + "/hostAPDU.html").c_str(), "wb"), "Host APDU"), - [](FILE* f) { - clog << "Closing host APDU stream" << endl; - closeHTML(f); - } +#ifndef MANUAL_LIFETIME + genHostPacketStream() +#endif }; +#ifndef MANUAL_LIFETIME if (!stream) clog << "Stream is unavailable" << endl; +#endif return stream; } -shared_ptr getComDevStream() { - static shared_ptr stream{ fopen((cacheDirectory + "/comdev.txt").c_str(), "wb"), - [](FILE* f) { - clog << "Closing comdev stream" << endl; - fclose(f); - } }; - - if (!stream) - clog << "Stream is unavailable" << endl; - - return stream; -} - -shared_ptr getDevPacketStream() { +shared_ptr& getHostAPDUStream() { static shared_ptr stream{ - initHTML(fopen((cacheDirectory + "/devpackets.html").c_str(), "wb"), "Dev Packets"), - [](FILE* f) { - clog << "Closing devPackets stream" << endl; - closeHTML(f); - } +#ifndef MANUAL_LIFETIME + genHostAPDUStream() +#endif }; +#ifndef MANUAL_LIFETIME if (!stream) clog << "Stream is unavailable" << endl; +#endif return stream; } -shared_ptr getDevAPDUStream() { - static shared_ptr stream{ initHTML(fopen((cacheDirectory + "/devAPDU.html").c_str(), "wb"), - "Dev APDU"), - [](FILE* f) { - clog << "Closing dev APDU stream" << endl; - closeHTML(f); - } }; +shared_ptr& getComDevStream() { + static shared_ptr stream{ +#ifndef MANUAL_LIFETIME + genComDevStream() +#endif + }; +#ifndef MANUAL_LIFETIME if (!stream) clog << "Stream is unavailable" << endl; +#endif + + return stream; +} + +shared_ptr& getDevPacketStream() { + static shared_ptr stream{ +#ifndef MANUAL_LIFETIME + genDevPacketStream() +#endif + }; + +#ifndef MANUAL_LIFETIME + if (!stream) + clog << "Stream is unavailable" << endl; +#endif + + return stream; +} + +shared_ptr& getDevAPDUStream() { + static shared_ptr stream{ +#ifndef MANUAL_LIFETIME + genDevAPDUStream() +#endif + }; + +#ifndef MANUAL_LIFETIME + if (!stream) + clog << "Stream is unavailable" << endl; +#endif return stream; } @@ -194,7 +210,7 @@ void closeHTML(FILE* fPtr) { #endif -shared_ptr initialiseHostDescriptor() { +shared_ptr genHostDescriptor() { int descriptor; #ifdef HID_SOCKET @@ -237,7 +253,7 @@ shared_ptr initialiseHostDescriptor() { close(*fd); remove(clientSocket.c_str()); delete fd; - } }; + } }; #else descriptor = open(HID_DEV, O_RDWR | O_NONBLOCK | O_APPEND); @@ -247,6 +263,81 @@ shared_ptr initialiseHostDescriptor() { return shared_ptr{ new int{ descriptor }, [](const int* fd) { close(*fd); delete fd; - } }; + } }; #endif } + +shared_ptr genComHostStream() { + return shared_ptr{ fopen((cacheDirectory + "/comhost.txt").c_str(), "wb"), + [](FILE* f) { + clog << "Closing comhost stream" << endl; + fclose(f); + } }; +} + +shared_ptr genHostPacketStream() { + return shared_ptr{ initHTML(fopen((cacheDirectory + "/hostpackets.html").c_str(), "wb"), "Host Packets"), + [](FILE* f) { + clog << "Closing hostPackets stream" << endl; + closeHTML(f); + } }; +} + +shared_ptr genHostAPDUStream() { + return shared_ptr{ initHTML(fopen((cacheDirectory + "/hostAPDU.html").c_str(), "wb"), "Host APDU"), + [](FILE* f) { + clog << "Closing host APDU stream" << endl; + closeHTML(f); + } }; +} + +shared_ptr genComDevStream() { + return shared_ptr{ fopen((cacheDirectory + "/comdev.txt").c_str(), "wb"), + [](FILE* f) { + clog << "Closing comdev stream" << endl; + fclose(f); + } }; +} + +shared_ptr genDevPacketStream() { + return shared_ptr{ initHTML(fopen((cacheDirectory + "/devpackets.html").c_str(), "wb"), "Dev Packets"), + [](FILE* f) { + clog << "Closing devPackets stream" << endl; + closeHTML(f); + } }; +} + +shared_ptr genDevAPDUStream() { + return shared_ptr{ initHTML(fopen((cacheDirectory + "/devAPDU.html").c_str(), "wb"), + "Dev APDU"), + [](FILE* f) { + clog << "Closing dev APDU stream" << endl; + closeHTML(f); + } }; +} + +#ifdef MANUAL_LIFETIME +void initStreams() { + getHostDescriptor() = genHostDescriptor(); + + getComHostStream() = genComHostStream(); + getHostPacketStream() = genHostPacketStream(); + getHostAPDUStream() = genHostAPDUStream(); + + getComDevStream() = genComDevStream(); + getDevPacketStream() = genDevPacketStream(); + getDevAPDUStream() = genDevAPDUStream(); +} + +void closeStreams() { + getHostDescriptor().reset(); + + getComHostStream().reset(); + getHostPacketStream().reset(); + getHostAPDUStream().reset(); + + getComDevStream().reset(); + getDevPacketStream().reset(); + getDevAPDUStream().reset(); +} +#endif diff --git a/Streams.hpp b/Streams.hpp index 7fbd4d9..acbb8f3 100644 --- a/Streams.hpp +++ b/Streams.hpp @@ -21,13 +21,18 @@ along with this program. If not, see . #include #include -std::shared_ptr getHostDescriptor(); +std::shared_ptr& getHostDescriptor(); + +#ifdef MANUAL_LIFETIME +void initStreams(); +void closeStreams(); +#endif #ifdef DEBUG_STREAMS -std::shared_ptr getComHostStream(); -std::shared_ptr getHostPacketStream(); -std::shared_ptr getHostAPDUStream(); -std::shared_ptr getComDevStream(); -std::shared_ptr getDevPacketStream(); -std::shared_ptr getDevAPDUStream(); +std::shared_ptr& getComHostStream(); +std::shared_ptr& getHostPacketStream(); +std::shared_ptr& getHostAPDUStream(); +std::shared_ptr& getComDevStream(); +std::shared_ptr& getDevPacketStream(); +std::shared_ptr& getDevAPDUStream(); #endif diff --git a/_Architecture.hpp b/_Architecture.hpp index 596d0df..34d6831 100644 --- a/_Architecture.hpp +++ b/_Architecture.hpp @@ -35,5 +35,6 @@ extern std::string hidDev; # define HID_DEV hidDev.c_str() # define DEBUG_STREAMS "/sdcard/log" # define HID_SOCKET +# define MANUAL_LIFETIME // #define DEBUG_MSGS #endif