Allow on-demand file loading and unloading.

This commit is contained in:
2019-09-10 21:42:04 +01:00
parent 3cc7032cf4
commit 4dac7dd014
3 changed files with 161 additions and 64 deletions

View File

@@ -36,7 +36,7 @@ using std::runtime_error;
using std::shared_ptr; using std::shared_ptr;
using std::string; using std::string;
shared_ptr<int> initialiseHostDescriptor(); shared_ptr<int> genHostDescriptor();
#ifdef DEBUG_STREAMS #ifdef DEBUG_STREAMS
@@ -44,96 +44,112 @@ FILE* initHTML(FILE* fPtr, const string& title);
void closeHTML(FILE* fPtr); void closeHTML(FILE* fPtr);
shared_ptr<FILE> genComHostStream();
shared_ptr<FILE> genHostPacketStream();
shared_ptr<FILE> genHostAPDUStream();
shared_ptr<FILE> genComDevStream();
shared_ptr<FILE> genDevPacketStream();
shared_ptr<FILE> genDevAPDUStream();
#endif #endif
shared_ptr<int> getHostDescriptor() { shared_ptr<int>& getHostDescriptor() {
static shared_ptr<int> descriptor{ initialiseHostDescriptor() }; static shared_ptr<int> descriptor{
#ifndef MANUAL_LIFETIME
genHostDescriptor()
#endif
};
return descriptor; return descriptor;
} }
#ifdef DEBUG_STREAMS #ifdef DEBUG_STREAMS
shared_ptr<FILE> getComHostStream() { shared_ptr<FILE>& getComHostStream() {
static shared_ptr<FILE> 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<FILE> getHostPacketStream() {
static shared_ptr<FILE> stream{ static shared_ptr<FILE> stream{
initHTML(fopen((cacheDirectory + "/hostpackets.html").c_str(), "wb"), "Host Packets"), #ifndef MANUAL_LIFETIME
[](FILE* f) { genComHostStream()
clog << "Closing hostPackets stream" << endl; #endif
closeHTML(f);
}
}; };
#ifndef MANUAL_LIFETIME
if (!stream) if (!stream)
clog << "Stream is unavailable" << endl; clog << "Stream is unavailable" << endl;
#endif
return stream; return stream;
} }
shared_ptr<FILE> getHostAPDUStream() { shared_ptr<FILE>& getHostPacketStream() {
static shared_ptr<FILE> stream{ static shared_ptr<FILE> stream{
initHTML(fopen((cacheDirectory + "/hostAPDU.html").c_str(), "wb"), "Host APDU"), #ifndef MANUAL_LIFETIME
[](FILE* f) { genHostPacketStream()
clog << "Closing host APDU stream" << endl; #endif
closeHTML(f);
}
}; };
#ifndef MANUAL_LIFETIME
if (!stream) if (!stream)
clog << "Stream is unavailable" << endl; clog << "Stream is unavailable" << endl;
#endif
return stream; return stream;
} }
shared_ptr<FILE> getComDevStream() { shared_ptr<FILE>& getHostAPDUStream() {
static shared_ptr<FILE> 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<FILE> getDevPacketStream() {
static shared_ptr<FILE> stream{ static shared_ptr<FILE> stream{
initHTML(fopen((cacheDirectory + "/devpackets.html").c_str(), "wb"), "Dev Packets"), #ifndef MANUAL_LIFETIME
[](FILE* f) { genHostAPDUStream()
clog << "Closing devPackets stream" << endl; #endif
closeHTML(f);
}
}; };
#ifndef MANUAL_LIFETIME
if (!stream) if (!stream)
clog << "Stream is unavailable" << endl; clog << "Stream is unavailable" << endl;
#endif
return stream; return stream;
} }
shared_ptr<FILE> getDevAPDUStream() { shared_ptr<FILE>& getComDevStream() {
static shared_ptr<FILE> stream{ initHTML(fopen((cacheDirectory + "/devAPDU.html").c_str(), "wb"), static shared_ptr<FILE> stream{
"Dev APDU"), #ifndef MANUAL_LIFETIME
[](FILE* f) { genComDevStream()
clog << "Closing dev APDU stream" << endl; #endif
closeHTML(f); };
} };
#ifndef MANUAL_LIFETIME
if (!stream) if (!stream)
clog << "Stream is unavailable" << endl; clog << "Stream is unavailable" << endl;
#endif
return stream;
}
shared_ptr<FILE>& getDevPacketStream() {
static shared_ptr<FILE> stream{
#ifndef MANUAL_LIFETIME
genDevPacketStream()
#endif
};
#ifndef MANUAL_LIFETIME
if (!stream)
clog << "Stream is unavailable" << endl;
#endif
return stream;
}
shared_ptr<FILE>& getDevAPDUStream() {
static shared_ptr<FILE> stream{
#ifndef MANUAL_LIFETIME
genDevAPDUStream()
#endif
};
#ifndef MANUAL_LIFETIME
if (!stream)
clog << "Stream is unavailable" << endl;
#endif
return stream; return stream;
} }
@@ -194,7 +210,7 @@ void closeHTML(FILE* fPtr) {
#endif #endif
shared_ptr<int> initialiseHostDescriptor() { shared_ptr<int> genHostDescriptor() {
int descriptor; int descriptor;
#ifdef HID_SOCKET #ifdef HID_SOCKET
@@ -250,3 +266,78 @@ shared_ptr<int> initialiseHostDescriptor() {
} }; } };
#endif #endif
} }
shared_ptr<FILE> genComHostStream() {
return shared_ptr<FILE>{ fopen((cacheDirectory + "/comhost.txt").c_str(), "wb"),
[](FILE* f) {
clog << "Closing comhost stream" << endl;
fclose(f);
} };
}
shared_ptr<FILE> genHostPacketStream() {
return shared_ptr<FILE>{ initHTML(fopen((cacheDirectory + "/hostpackets.html").c_str(), "wb"), "Host Packets"),
[](FILE* f) {
clog << "Closing hostPackets stream" << endl;
closeHTML(f);
} };
}
shared_ptr<FILE> genHostAPDUStream() {
return shared_ptr<FILE>{ initHTML(fopen((cacheDirectory + "/hostAPDU.html").c_str(), "wb"), "Host APDU"),
[](FILE* f) {
clog << "Closing host APDU stream" << endl;
closeHTML(f);
} };
}
shared_ptr<FILE> genComDevStream() {
return shared_ptr<FILE>{ fopen((cacheDirectory + "/comdev.txt").c_str(), "wb"),
[](FILE* f) {
clog << "Closing comdev stream" << endl;
fclose(f);
} };
}
shared_ptr<FILE> genDevPacketStream() {
return shared_ptr<FILE>{ initHTML(fopen((cacheDirectory + "/devpackets.html").c_str(), "wb"), "Dev Packets"),
[](FILE* f) {
clog << "Closing devPackets stream" << endl;
closeHTML(f);
} };
}
shared_ptr<FILE> genDevAPDUStream() {
return shared_ptr<FILE>{ 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

View File

@@ -21,13 +21,18 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <cstdio> #include <cstdio>
#include <memory> #include <memory>
std::shared_ptr<int> getHostDescriptor(); std::shared_ptr<int>& getHostDescriptor();
#ifdef MANUAL_LIFETIME
void initStreams();
void closeStreams();
#endif
#ifdef DEBUG_STREAMS #ifdef DEBUG_STREAMS
std::shared_ptr<FILE> getComHostStream(); std::shared_ptr<FILE>& getComHostStream();
std::shared_ptr<FILE> getHostPacketStream(); std::shared_ptr<FILE>& getHostPacketStream();
std::shared_ptr<FILE> getHostAPDUStream(); std::shared_ptr<FILE>& getHostAPDUStream();
std::shared_ptr<FILE> getComDevStream(); std::shared_ptr<FILE>& getComDevStream();
std::shared_ptr<FILE> getDevPacketStream(); std::shared_ptr<FILE>& getDevPacketStream();
std::shared_ptr<FILE> getDevAPDUStream(); std::shared_ptr<FILE>& getDevAPDUStream();
#endif #endif

View File

@@ -35,5 +35,6 @@ extern std::string hidDev;
# define HID_DEV hidDev.c_str() # define HID_DEV hidDev.c_str()
# define DEBUG_STREAMS "/sdcard/log" # define DEBUG_STREAMS "/sdcard/log"
# define HID_SOCKET # define HID_SOCKET
# define MANUAL_LIFETIME
// #define DEBUG_MSGS // #define DEBUG_MSGS
#endif #endif