mayhem-firmware/firmware/application/capture_thread.hpp

151 lines
3.4 KiB
C++
Raw Normal View History

2016-02-10 01:49:17 +00:00
/*
* Copyright (C) 2016 Jared Boone, ShareBrained Technology, Inc.
*
* This file is part of PortaPack.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#ifndef __CAPTURE_THREAD_H__
#define __CAPTURE_THREAD_H__
2016-02-10 01:49:17 +00:00
#include "ch.h"
#include "file.hpp"
#include "event_m0.hpp"
#include "portapack_shared_memory.hpp"
#include "hackrf_gpio.hpp"
using namespace hackrf::one;
#include <cstring>
class StreamOutput {
public:
StreamOutput() {
shared_memory.baseband_queue.push_and_wait(
CaptureConfigMessage { &config }
);
}
~StreamOutput() {
shared_memory.baseband_queue.push_and_wait(
CaptureConfigMessage { nullptr }
);
2016-02-10 01:49:17 +00:00
}
size_t available() {
return config.fifo->len();
2016-02-10 01:49:17 +00:00
}
size_t read(void* const data, const size_t length) {
return config.fifo->out(reinterpret_cast<uint8_t*>(data), length);
2016-02-10 01:49:17 +00:00
}
private:
CaptureConfig config;
2016-02-10 01:49:17 +00:00
};
class CaptureThread {
2016-02-10 01:49:17 +00:00
public:
CaptureThread(
2016-02-10 01:49:17 +00:00
std::string file_path
) : file_path { std::move(file_path) },
write_buffer { std::make_unique<std::array<uint8_t, write_size>>() }
2016-02-10 01:49:17 +00:00
{
// Need significant stack for FATFS
thread = chThdCreateFromHeap(NULL, 1024, NORMALPRIO + 10, CaptureThread::static_fn, this);
2016-02-10 01:49:17 +00:00
}
~CaptureThread() {
const auto thread_tmp = thread;
2016-04-06 18:47:25 +00:00
if( thread_tmp ) {
thread = nullptr;
chThdTerminate(thread_tmp);
chEvtSignal(thread_tmp, EVT_FIFO_HIGHWATER);
const auto success = chThdWait(thread_tmp);
if( !success ) {
led_tx.on();
}
2016-04-06 18:47:25 +00:00
}
2016-02-10 01:49:17 +00:00
}
static void check_fifo_isr() {
// TODO: Prevent over-signalling by transmitting a set of
// flags from the baseband core.
if( thread ) {
chEvtSignalI(thread, EVT_FIFO_HIGHWATER);
}
}
2016-02-10 01:49:17 +00:00
private:
static constexpr size_t write_size = 16384;
static constexpr eventmask_t EVT_FIFO_HIGHWATER = 1;
const std::string file_path;
std::unique_ptr<std::array<uint8_t, write_size>> write_buffer;
2016-02-10 01:49:17 +00:00
File file;
static Thread* thread;
static msg_t static_fn(void* arg) {
auto obj = static_cast<CaptureThread*>(arg);
2016-04-06 18:47:25 +00:00
return obj->run();
2016-02-10 01:49:17 +00:00
}
2016-04-06 18:47:25 +00:00
msg_t run() {
if( !file.open_for_writing(file_path) ) {
return false;
}
StreamOutput stream;
2016-02-10 01:49:17 +00:00
2016-04-06 18:52:05 +00:00
while( !chThdShouldTerminate() ) {
chEvtWaitAny(EVT_FIFO_HIGHWATER);
2016-02-10 01:49:17 +00:00
2016-04-06 18:52:05 +00:00
while( stream.available() >= write_buffer->size() ) {
if( !transfer(stream, write_buffer.get()) ) {
return false;
}
}
}
2016-04-06 18:52:05 +00:00
return true;
}
bool transfer(StreamOutput& stream, std::array<uint8_t, write_size>* const write_buffer) {
bool success = false;
2016-02-10 01:49:17 +00:00
led_usb.on();
2016-02-10 01:49:17 +00:00
const auto bytes_to_write = stream.read(write_buffer->data(), write_buffer->size());
if( bytes_to_write == write_buffer->size() ) {
if( file.write(write_buffer->data(), write_buffer->size()) ) {
success = true;
2016-02-10 01:49:17 +00:00
}
}
led_usb.off();
return success;
2016-02-10 01:49:17 +00:00
}
};
#endif/*__CAPTURE_THREAD_H__*/