2016-02-09 20:49:17 -05: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.
|
|
|
|
*/
|
|
|
|
|
2016-04-22 15:15:51 -04:00
|
|
|
#include "capture_thread.hpp"
|
2016-02-09 20:49:17 -05:00
|
|
|
|
2016-06-20 13:52:59 -04:00
|
|
|
#include "baseband_api.hpp"
|
2016-05-02 15:50:49 -04:00
|
|
|
|
|
|
|
// StreamOutput ///////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class StreamOutput {
|
|
|
|
public:
|
|
|
|
StreamOutput(CaptureConfig* const config);
|
|
|
|
~StreamOutput();
|
|
|
|
|
|
|
|
size_t available() {
|
2016-05-10 17:12:37 -04:00
|
|
|
return fifo_buffers_full->len();
|
2016-05-02 15:50:49 -04:00
|
|
|
}
|
|
|
|
|
2016-05-10 17:12:37 -04:00
|
|
|
StreamBuffer* get_buffer() {
|
|
|
|
StreamBuffer* p { nullptr };
|
|
|
|
fifo_buffers_full->out(p);
|
|
|
|
return p;
|
2016-05-02 15:50:49 -04:00
|
|
|
}
|
|
|
|
|
2016-05-10 17:12:37 -04:00
|
|
|
bool release_buffer(StreamBuffer* const p) {
|
|
|
|
p->empty();
|
|
|
|
return fifo_buffers_empty->in(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
static FIFO<StreamBuffer*>* fifo_buffers_empty;
|
|
|
|
static FIFO<StreamBuffer*>* fifo_buffers_full;
|
2016-05-02 15:50:49 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
CaptureConfig* const config;
|
|
|
|
};
|
|
|
|
|
2016-05-10 17:12:37 -04:00
|
|
|
FIFO<StreamBuffer*>* StreamOutput::fifo_buffers_empty = nullptr;
|
|
|
|
FIFO<StreamBuffer*>* StreamOutput::fifo_buffers_full = nullptr;
|
2016-05-02 15:50:49 -04:00
|
|
|
|
|
|
|
StreamOutput::StreamOutput(
|
|
|
|
CaptureConfig* const config
|
|
|
|
) : config { config }
|
|
|
|
{
|
2016-06-20 13:52:59 -04:00
|
|
|
baseband::capture_start(config);
|
2016-05-10 17:12:37 -04:00
|
|
|
fifo_buffers_empty = config->fifo_buffers_empty;
|
|
|
|
fifo_buffers_full = config->fifo_buffers_full;
|
2016-05-02 15:50:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
StreamOutput::~StreamOutput() {
|
2016-05-10 17:12:37 -04:00
|
|
|
fifo_buffers_full = nullptr;
|
|
|
|
fifo_buffers_empty = nullptr;
|
2016-06-20 13:52:59 -04:00
|
|
|
baseband::capture_stop();
|
2016-05-02 15:50:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CaptureThread //////////////////////////////////////////////////////////
|
|
|
|
|
2016-04-22 15:15:51 -04:00
|
|
|
Thread* CaptureThread::thread = nullptr;
|
2016-05-02 15:50:49 -04:00
|
|
|
|
|
|
|
CaptureThread::CaptureThread(
|
|
|
|
std::unique_ptr<Writer> writer,
|
2016-05-10 17:12:37 -04:00
|
|
|
size_t write_size,
|
2016-06-21 14:04:10 -04:00
|
|
|
size_t buffer_count,
|
2016-06-21 14:53:07 -04:00
|
|
|
std::function<void()> success_callback,
|
2016-06-21 14:52:05 -04:00
|
|
|
std::function<void(File::Error)> error_callback
|
2016-05-10 17:12:37 -04:00
|
|
|
) : config { write_size, buffer_count },
|
2016-06-21 14:04:10 -04:00
|
|
|
writer { std::move(writer) },
|
2016-06-21 14:53:07 -04:00
|
|
|
success_callback { std::move(success_callback) },
|
2016-06-21 14:04:10 -04:00
|
|
|
error_callback { std::move(error_callback) }
|
2016-05-02 15:50:49 -04:00
|
|
|
{
|
|
|
|
// Need significant stack for FATFS
|
|
|
|
thread = chThdCreateFromHeap(NULL, 1024, NORMALPRIO + 10, CaptureThread::static_fn, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
CaptureThread::~CaptureThread() {
|
|
|
|
if( thread ) {
|
|
|
|
chThdTerminate(thread);
|
2016-06-19 14:20:45 -04:00
|
|
|
chEvtSignal(thread, event_mask_loop_wake);
|
2016-05-02 15:51:52 -04:00
|
|
|
chThdWait(thread);
|
2016-05-02 15:50:49 -04:00
|
|
|
thread = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CaptureThread::check_fifo_isr() {
|
|
|
|
// TODO: Prevent over-signalling by transmitting a set of
|
|
|
|
// flags from the baseband core.
|
2016-05-10 17:12:37 -04:00
|
|
|
const auto fifo = StreamOutput::fifo_buffers_full;
|
2016-05-02 15:50:49 -04:00
|
|
|
if( fifo ) {
|
2016-05-10 17:39:24 -04:00
|
|
|
if( !fifo->is_empty() ) {
|
2016-06-19 14:20:45 -04:00
|
|
|
chEvtSignalI(thread, event_mask_loop_wake);
|
2016-05-10 17:39:24 -04:00
|
|
|
}
|
2016-05-02 15:50:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-21 15:15:57 -04:00
|
|
|
msg_t CaptureThread::static_fn(void* arg) {
|
|
|
|
auto obj = static_cast<CaptureThread*>(arg);
|
|
|
|
const auto error = obj->run();
|
|
|
|
if( error.is_valid() && obj->error_callback ) {
|
|
|
|
obj->error_callback(error.value());
|
|
|
|
} else {
|
|
|
|
if( obj->success_callback ) {
|
|
|
|
obj->success_callback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:01:44 -04:00
|
|
|
Optional<File::Error> CaptureThread::run() {
|
2016-05-02 15:50:49 -04:00
|
|
|
StreamOutput stream { &config };
|
|
|
|
|
|
|
|
while( !chThdShouldTerminate() ) {
|
2016-05-10 17:12:37 -04:00
|
|
|
if( stream.available() ) {
|
|
|
|
auto buffer = stream.get_buffer();
|
2016-05-16 17:01:44 -04:00
|
|
|
auto write_result = writer->write(buffer->data(), buffer->size());
|
|
|
|
if( write_result.is_error() ) {
|
|
|
|
return write_result.error();
|
2016-05-02 15:50:49 -04:00
|
|
|
}
|
2016-05-10 17:12:37 -04:00
|
|
|
stream.release_buffer(buffer);
|
2016-05-02 15:50:49 -04:00
|
|
|
} else {
|
2016-06-19 14:20:45 -04:00
|
|
|
chEvtWaitAny(event_mask_loop_wake);
|
2016-05-02 15:50:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:01:44 -04:00
|
|
|
return { };
|
2016-05-02 15:50:49 -04:00
|
|
|
}
|