Reformatting.
This commit is contained in:
74
IO.cpp
74
IO.cpp
@@ -17,24 +17,24 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "IO.hpp"
|
||||
#include "Streams.hpp"
|
||||
#include <iostream>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <ctime>
|
||||
#include <chrono>
|
||||
#include <ratio>
|
||||
#include <array>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
#include <memory>
|
||||
#include <cstdio>
|
||||
#include <android/log.h>
|
||||
#include "u2f.hpp"
|
||||
#include "Macro.hpp"
|
||||
#include "Streams.hpp"
|
||||
#include "U2FDevice.hpp"
|
||||
#include "u2f.hpp"
|
||||
#include <android/log.h>
|
||||
#include <array>
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <ctime>
|
||||
#include <fcntl.h>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <ratio>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -45,15 +45,14 @@ string binaryDirectory{};
|
||||
string cacheDirectory{ DEBUG_STREAMS };
|
||||
|
||||
// Thanks to https://stackoverflow.com/a/478960
|
||||
vector<uint8_t> execOutput(const string &cmd);
|
||||
void execInput(const string &cmd, const vector<uint8_t> &stdinData);
|
||||
vector<uint8_t> execOutput(const string& cmd);
|
||||
void execInput(const string& cmd, const vector<uint8_t>& stdinData);
|
||||
|
||||
vector<uint8_t> readNonBlock(const size_t count)
|
||||
{
|
||||
vector<uint8_t> readNonBlock(const size_t count) {
|
||||
if (!bytesAvailable(count))
|
||||
return vector<uint8_t>{};
|
||||
|
||||
auto &buffer = getBuffer();
|
||||
auto& buffer = getBuffer();
|
||||
auto buffStart = buffer.begin(), buffEnd = buffer.begin() + count;
|
||||
vector<uint8_t> bytes{ buffStart, buffEnd };
|
||||
buffer.erase(buffStart, buffEnd);
|
||||
@@ -63,20 +62,17 @@ vector<uint8_t> readNonBlock(const size_t count)
|
||||
return bytes;
|
||||
}
|
||||
|
||||
void write(const vector<uint8_t> &bytes)
|
||||
{
|
||||
__android_log_print(ANDROID_LOG_DEBUG, "U2FAndroid", "Writing %zu bytes", bytes.size());
|
||||
void write(const vector<uint8_t>& bytes) {
|
||||
__android_log_print(ANDROID_LOG_DEBUG, "U2FAndroid", "Writing %zu bytes", bytes.size());
|
||||
execInput("su -c \"" + binaryDirectory + "/U2FAndroid_Write\"", bytes);
|
||||
}
|
||||
|
||||
bool bytesAvailable(const size_t count)
|
||||
{
|
||||
bool bytesAvailable(const size_t count) {
|
||||
auto startTime = std::chrono::high_resolution_clock::now();
|
||||
const timespec iterDelay{ 0, 10000000 };
|
||||
chrono::duration<double, milli> delay{ 0 };
|
||||
|
||||
while (delay.count() < U2FHID_TRANS_TIMEOUT)
|
||||
{
|
||||
while (delay.count() < U2FHID_TRANS_TIMEOUT) {
|
||||
if (getBuffer().size() >= count) {
|
||||
#ifdef DEBUG_MSGS
|
||||
clog << "Requested " << count << " bytes" << endl;
|
||||
@@ -84,7 +80,7 @@ bool bytesAvailable(const size_t count)
|
||||
return true;
|
||||
}
|
||||
nanosleep(&iterDelay, nullptr);
|
||||
delay = chrono::high_resolution_clock::now() - startTime;
|
||||
delay = chrono::high_resolution_clock::now() - startTime;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_MSGS
|
||||
@@ -94,20 +90,18 @@ bool bytesAvailable(const size_t count)
|
||||
return false;
|
||||
}
|
||||
|
||||
vector<uint8_t>& bufferVar()
|
||||
{
|
||||
vector<uint8_t>& bufferVar() {
|
||||
static vector<uint8_t> buffer{};
|
||||
return buffer;
|
||||
}
|
||||
|
||||
vector<uint8_t>& getBuffer()
|
||||
{
|
||||
auto &buff = bufferVar();
|
||||
vector<uint8_t>& getBuffer() {
|
||||
auto& buff = bufferVar();
|
||||
vector<uint8_t> bytes = execOutput("su -c \"" + binaryDirectory + "/U2FAndroid_Read\"");
|
||||
|
||||
if (!bytes.empty())
|
||||
{
|
||||
__android_log_print(ANDROID_LOG_DEBUG, "U2FAndroid", "Reading bytes: got %zu", bytes.size());
|
||||
if (!bytes.empty()) {
|
||||
__android_log_print(ANDROID_LOG_DEBUG, "U2FAndroid", "Reading bytes: got %zu",
|
||||
bytes.size());
|
||||
buff.insert(buff.end(), bytes.begin(), bytes.end());
|
||||
|
||||
#ifdef DEBUG_STREAMS
|
||||
@@ -118,8 +112,7 @@ vector<uint8_t>& getBuffer()
|
||||
return buff;
|
||||
}
|
||||
|
||||
vector<uint8_t> execOutput(const string &cmd)
|
||||
{
|
||||
vector<uint8_t> execOutput(const string& cmd) {
|
||||
// NOLINT(hicpp-member-init)
|
||||
array<char, HID_RPT_SIZE> buffer;
|
||||
vector<uint8_t> result{};
|
||||
@@ -133,8 +126,7 @@ vector<uint8_t> execOutput(const string &cmd)
|
||||
return result;
|
||||
}
|
||||
|
||||
void execInput(const string &cmd, const vector<uint8_t> &stdinData)
|
||||
{
|
||||
void execInput(const string& cmd, const vector<uint8_t>& stdinData) {
|
||||
assert(stdinData.size() % HID_RPT_SIZE == 0);
|
||||
|
||||
size_t writtenBytes = 0;
|
||||
|
||||
Reference in New Issue
Block a user