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
|
|
|
#ifndef __CAPTURE_THREAD_H__
|
|
|
|
#define __CAPTURE_THREAD_H__
|
2016-02-09 20:49:17 -05: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>
|
|
|
|
|
2016-04-30 19:34:50 -04:00
|
|
|
class Writer {
|
|
|
|
public:
|
|
|
|
virtual bool write(const void* const write_buffer, const size_t write_size) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class RawFileWriter : public Writer {
|
|
|
|
public:
|
|
|
|
RawFileWriter(
|
|
|
|
const std::string& filename
|
|
|
|
) : file { filename, File::openmode::out | File::openmode::binary | File::openmode::trunc }
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool write(const void* const write_buffer, const size_t write_size) override {
|
|
|
|
return file.write(write_buffer, write_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
File file;
|
|
|
|
};
|
|
|
|
|
2016-02-09 20:49:17 -05:00
|
|
|
class StreamOutput {
|
|
|
|
public:
|
2016-04-27 01:26:30 -04:00
|
|
|
StreamOutput(
|
2016-04-27 13:56:50 -04:00
|
|
|
CaptureConfig* const config
|
|
|
|
) : config { config }
|
2016-04-27 01:26:30 -04:00
|
|
|
{
|
2016-04-23 18:07:44 -04:00
|
|
|
shared_memory.baseband_queue.push_and_wait(
|
2016-04-27 13:56:50 -04:00
|
|
|
CaptureConfigMessage { config }
|
2016-04-23 18:07:44 -04:00
|
|
|
);
|
2016-04-27 13:56:50 -04:00
|
|
|
fifo = config->fifo;
|
2016-04-23 18:07:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
~StreamOutput() {
|
2016-04-27 01:26:30 -04:00
|
|
|
fifo = nullptr;
|
2016-04-23 18:07:44 -04:00
|
|
|
shared_memory.baseband_queue.push_and_wait(
|
|
|
|
CaptureConfigMessage { nullptr }
|
|
|
|
);
|
2016-02-09 20:49:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t available() {
|
2016-04-27 13:52:11 -04:00
|
|
|
return fifo->len();
|
2016-02-09 20:49:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t read(void* const data, const size_t length) {
|
2016-04-27 13:52:11 -04:00
|
|
|
return fifo->out(reinterpret_cast<uint8_t*>(data), length);
|
2016-02-09 20:49:17 -05:00
|
|
|
}
|
|
|
|
|
2016-04-27 01:26:30 -04:00
|
|
|
static FIFO<uint8_t>* fifo;
|
|
|
|
|
2016-02-09 20:49:17 -05:00
|
|
|
private:
|
2016-04-27 13:56:50 -04:00
|
|
|
CaptureConfig* const config;
|
2016-02-09 20:49:17 -05:00
|
|
|
};
|
|
|
|
|
2016-04-22 15:15:51 -04:00
|
|
|
class CaptureThread {
|
2016-02-09 20:49:17 -05:00
|
|
|
public:
|
2016-04-22 15:15:51 -04:00
|
|
|
CaptureThread(
|
2016-04-30 19:34:50 -04:00
|
|
|
std::unique_ptr<Writer> writer,
|
2016-04-27 01:26:30 -04:00
|
|
|
size_t write_size_log2,
|
|
|
|
size_t buffer_count_log2
|
2016-04-27 13:56:50 -04:00
|
|
|
) : config { write_size_log2, buffer_count_log2 },
|
2016-04-30 19:34:50 -04:00
|
|
|
writer { std::move(writer) }
|
2016-02-09 20:49:17 -05:00
|
|
|
{
|
|
|
|
// Need significant stack for FATFS
|
2016-04-30 19:55:52 -04:00
|
|
|
thread = chThdCreateFromHeap(NULL, 1024, NORMALPRIO + 10, CaptureThread::static_fn, this);
|
2016-02-09 20:49:17 -05:00
|
|
|
}
|
|
|
|
|
2016-04-22 15:15:51 -04:00
|
|
|
~CaptureThread() {
|
2016-04-27 13:13:50 -04:00
|
|
|
if( thread ) {
|
|
|
|
chThdTerminate(thread);
|
|
|
|
chEvtSignal(thread, EVT_MASK_CAPTURE_THREAD);
|
|
|
|
const auto success = chThdWait(thread);
|
2016-04-23 18:07:44 -04:00
|
|
|
thread = nullptr;
|
|
|
|
|
|
|
|
if( !success ) {
|
|
|
|
led_tx.on();
|
|
|
|
}
|
2016-04-06 14:47:25 -04:00
|
|
|
}
|
2016-02-09 20:49:17 -05:00
|
|
|
}
|
|
|
|
|
2016-04-27 15:06:47 -04:00
|
|
|
const CaptureConfig& state() const {
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2016-03-28 01:49:30 -04:00
|
|
|
static void check_fifo_isr() {
|
2016-04-23 18:07:44 -04:00
|
|
|
// TODO: Prevent over-signalling by transmitting a set of
|
|
|
|
// flags from the baseband core.
|
2016-04-27 01:26:30 -04:00
|
|
|
const auto fifo = StreamOutput::fifo;
|
|
|
|
if( fifo ) {
|
2016-04-23 20:28:31 -04:00
|
|
|
chEvtSignalI(thread, EVT_MASK_CAPTURE_THREAD);
|
2016-03-28 01:49:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-09 20:49:17 -05:00
|
|
|
private:
|
2016-04-27 13:56:50 -04:00
|
|
|
CaptureConfig config;
|
2016-04-30 19:34:50 -04:00
|
|
|
std::unique_ptr<Writer> writer;
|
2016-02-09 20:49:17 -05:00
|
|
|
static Thread* thread;
|
|
|
|
|
|
|
|
static msg_t static_fn(void* arg) {
|
2016-04-22 15:15:51 -04:00
|
|
|
auto obj = static_cast<CaptureThread*>(arg);
|
2016-04-06 14:47:25 -04:00
|
|
|
return obj->run();
|
2016-02-09 20:49:17 -05:00
|
|
|
}
|
|
|
|
|
2016-04-06 14:47:25 -04:00
|
|
|
msg_t run() {
|
2016-04-27 13:56:50 -04:00
|
|
|
const size_t write_size = 1U << config.write_size_log2;
|
2016-04-27 01:26:30 -04:00
|
|
|
const auto write_buffer = std::make_unique<uint8_t[]>(write_size);
|
|
|
|
if( !write_buffer ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-27 13:56:50 -04:00
|
|
|
StreamOutput stream { &config };
|
2016-02-09 20:49:17 -05:00
|
|
|
|
2016-04-06 14:52:05 -04:00
|
|
|
while( !chThdShouldTerminate() ) {
|
2016-04-27 13:12:21 -04:00
|
|
|
if( stream.available() >= write_size ) {
|
2016-04-30 19:26:49 -04:00
|
|
|
if( !transfer(stream, write_buffer.get(), write_size) ) {
|
2016-04-06 14:52:05 -04:00
|
|
|
return false;
|
|
|
|
}
|
2016-04-27 13:12:21 -04:00
|
|
|
} else {
|
|
|
|
chEvtWaitAny(EVT_MASK_CAPTURE_THREAD);
|
2016-04-06 14:35:00 -04:00
|
|
|
}
|
|
|
|
}
|
2016-04-06 14:52:05 -04:00
|
|
|
|
|
|
|
return true;
|
2016-04-06 14:35:00 -04:00
|
|
|
}
|
|
|
|
|
2016-04-30 19:26:49 -04:00
|
|
|
bool transfer(StreamOutput& stream, uint8_t* const write_buffer, const size_t write_size) {
|
2016-04-06 14:35:00 -04:00
|
|
|
bool success = false;
|
2016-02-09 20:49:17 -05:00
|
|
|
|
2016-04-06 14:35:00 -04:00
|
|
|
led_usb.on();
|
2016-02-09 20:49:17 -05:00
|
|
|
|
2016-04-27 01:26:30 -04:00
|
|
|
const auto bytes_to_write = stream.read(write_buffer, write_size);
|
|
|
|
if( bytes_to_write == write_size ) {
|
2016-04-30 19:34:50 -04:00
|
|
|
if( writer->write(write_buffer, write_size) ) {
|
2016-04-06 14:35:00 -04:00
|
|
|
success = true;
|
2016-02-09 20:49:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-06 14:35:00 -04:00
|
|
|
led_usb.off();
|
|
|
|
|
|
|
|
return success;
|
2016-02-09 20:49:17 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-22 15:15:51 -04:00
|
|
|
#endif/*__CAPTURE_THREAD_H__*/
|