2017-01-10 14:45:40 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
* Copyright (C) 2016 Furrtek
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "replay_thread.hpp"
|
|
|
|
|
|
|
|
#include "baseband_api.hpp"
|
2017-01-15 22:45:44 -05:00
|
|
|
#include "buffer_exchange.hpp"
|
2017-01-10 14:45:40 -05:00
|
|
|
|
2017-01-15 22:45:44 -05:00
|
|
|
struct BasebandReplay {
|
2017-04-19 17:05:16 -04:00
|
|
|
BasebandReplay(ReplayConfig* const config) {
|
2017-01-15 22:45:44 -05:00
|
|
|
baseband::replay_start(config);
|
2017-01-10 14:45:40 -05:00
|
|
|
}
|
|
|
|
|
2017-01-15 22:45:44 -05:00
|
|
|
~BasebandReplay() {
|
|
|
|
baseband::replay_stop();
|
2017-01-10 14:45:40 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-15 22:45:44 -05:00
|
|
|
// ReplayThread ///////////////////////////////////////////////////////////
|
2017-01-10 14:45:40 -05:00
|
|
|
|
|
|
|
ReplayThread::ReplayThread(
|
2017-01-15 22:45:44 -05:00
|
|
|
std::unique_ptr<stream::Reader> reader,
|
2017-01-10 14:45:40 -05:00
|
|
|
size_t read_size,
|
|
|
|
size_t buffer_count,
|
|
|
|
std::function<void()> success_callback,
|
|
|
|
std::function<void(File::Error)> error_callback
|
|
|
|
) : config { read_size, buffer_count },
|
|
|
|
reader { std::move(reader) },
|
|
|
|
success_callback { std::move(success_callback) },
|
|
|
|
error_callback { std::move(error_callback) }
|
|
|
|
{
|
|
|
|
// Need significant stack for FATFS
|
|
|
|
thread = chThdCreateFromHeap(NULL, 1024, NORMALPRIO + 10, ReplayThread::static_fn, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
ReplayThread::~ReplayThread() {
|
|
|
|
if( thread ) {
|
|
|
|
chThdTerminate(thread);
|
|
|
|
chThdWait(thread);
|
|
|
|
thread = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
msg_t ReplayThread::static_fn(void* arg) {
|
|
|
|
auto obj = static_cast<ReplayThread*>(arg);
|
|
|
|
const auto error = obj->run();
|
2017-04-21 01:22:31 -04:00
|
|
|
if( error.is_valid() && obj->error_callback ) {
|
2017-01-10 14:45:40 -05:00
|
|
|
obj->error_callback(error.value());
|
|
|
|
} else {
|
|
|
|
if( obj->success_callback ) {
|
|
|
|
obj->success_callback();
|
|
|
|
}
|
2017-04-21 01:22:31 -04:00
|
|
|
}
|
2017-01-10 14:45:40 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
Optional<File::Error> ReplayThread::run() {
|
2017-01-15 22:45:44 -05:00
|
|
|
BasebandReplay replay { &config };
|
|
|
|
BufferExchange buffers { &config };
|
2017-12-06 08:20:51 -05:00
|
|
|
|
|
|
|
StreamBuffer* prefill_buffer { nullptr };
|
|
|
|
|
|
|
|
// TESTING: Prefill
|
|
|
|
// While empty buffers fifo is not empty...
|
|
|
|
while (!buffers.empty()) {
|
|
|
|
prefill_buffer = buffers.get_prefill();
|
|
|
|
|
|
|
|
if (prefill_buffer == nullptr) {
|
|
|
|
buffers.put_app(prefill_buffer);
|
|
|
|
} else {
|
|
|
|
size_t blocks = prefill_buffer->capacity() / 512;
|
|
|
|
|
|
|
|
for (size_t c = 0; c < blocks; c++) {
|
|
|
|
auto read_result = reader->read(&((uint8_t*)prefill_buffer->data())[c * 512], 512);
|
|
|
|
if( read_result.is_error() ) {
|
|
|
|
return read_result.error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prefill_buffer->set_size(prefill_buffer->capacity());
|
|
|
|
|
|
|
|
buffers.put(prefill_buffer);
|
|
|
|
//if (!buffers.put(prefill_buffer)) for(;;) {};
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
2017-01-10 14:45:40 -05:00
|
|
|
|
|
|
|
while( !chThdShouldTerminate() ) {
|
2017-04-21 01:22:31 -04:00
|
|
|
auto buffer = buffers.get();
|
2017-06-23 03:40:22 -04:00
|
|
|
|
2017-12-06 08:20:51 -05:00
|
|
|
size_t blocks = buffer->capacity() / 512;
|
|
|
|
|
|
|
|
for (size_t c = 0; c < blocks; c++) {
|
|
|
|
auto read_result = reader->read(&((uint8_t*)buffer->data())[c * 512], 512);
|
|
|
|
if( read_result.is_error() ) {
|
|
|
|
return read_result.error();
|
|
|
|
}
|
2017-01-10 14:45:40 -05:00
|
|
|
}
|
2017-06-23 03:40:22 -04:00
|
|
|
|
2017-12-06 08:20:51 -05:00
|
|
|
buffer->set_size(buffer->capacity());
|
|
|
|
|
2017-06-24 13:42:41 -04:00
|
|
|
buffers.put(buffer);
|
2017-01-10 14:45:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return { };
|
|
|
|
}
|