Progress on adding file pointers

This commit is contained in:
30hours 2024-01-19 05:56:49 +00:00
parent 7d42fb2e33
commit 703560e683
8 changed files with 93 additions and 133 deletions

View file

@ -25,16 +25,6 @@ void Capture::process(IqData *buffer1, IqData *buffer2, c4::yml::NodeRef config)
std::unique_ptr<Source> device = factory_source(type, config);
if (!replay)
{
device->start();
device->process(buffer1, buffer2);
}
else
{
device->replay(buffer1, buffer2, file, loop);
}
// capture status thread
std::thread t1([&]{
while (true)
@ -55,10 +45,20 @@ void Capture::process(IqData *buffer1, IqData *buffer2, c4::yml::NodeRef config)
device->close_file();
}
}
sleep(1);
}
});
if (!replay)
{
device->start();
device->process(buffer1, buffer2);
}
else
{
device->replay(buffer1, buffer2, file, loop);
}
}
std::unique_ptr<Source> Capture::factory_source(const std::string& type, c4::yml::NodeRef config)
@ -99,18 +99,3 @@ void Capture::set_replay(bool _loop, std::string _file)
loop = _loop;
file = _file;
}
bool Capture::is_type_valid(std::string _type)
{
size_t n = sizeof(Capture::VALID_TYPE) /
sizeof(Capture::VALID_TYPE[0]);
for (size_t i = 0; i < n; i++)
{
if (_type == Capture::VALID_TYPE[i])
{
return true;
}
}
std::cerr << "Invalid capture device: " << _type << std::endl;
return false;
}

View file

@ -21,6 +21,9 @@ private:
/// @brief The valid capture devices.
static const std::string VALID_TYPE[2];
/// @brief The capture device type.
std::string type;
/// @brief True if IQ data to be saved.
bool saveIq;
@ -34,8 +37,6 @@ private:
std::string file;
public:
/// @brief The capture device type.
std::string type;
/// @brief Sampling frequency (Hz).
uint32_t fs;
@ -70,10 +71,6 @@ public:
/// @return Void.
void set_replay(bool loop, std::string file);
/// @brief Check if capture device type is valid.
/// @param type The capture device type.
/// @return True is capture device type is valid.
bool is_type_valid(std::string type);
};
#endif

View file

@ -1,10 +1,14 @@
#include "Source.h"
#include <iostream>
#include <algorithm>
#include <chrono>
#include <ctime>
#include <sstream>
#include <iomanip>
Source::Source()
{
}
// constructor
@ -16,4 +20,45 @@ Source::Source(std::string _type, uint32_t _fc, uint32_t _fs,
fs = _fs;
path = _path;
saveIq = _saveIq;
saveIqFile = NULL;
}
std::string Source::open_file()
{
// get string of timestamp in YYYYmmdd-HHMMSS
auto currentTime = std::chrono::system_clock::to_time_t(
std::chrono::system_clock::now());
std::tm* timeInfo = std::localtime(&currentTime);
std::ostringstream oss;
oss << std::put_time(timeInfo, "%Y%m%d-%H%M%S");
std::string timestamp = oss.str();
// create file path
std::string typeLower = type;
std::transform(typeLower.begin(), typeLower.end(),
typeLower.begin(), ::tolower);
std::string file = path + timestamp + "." + typeLower + ".iq";
saveIqFile = fopen(file.c_str(), "wb");
if (saveIqFile == NULL)
{
std::cerr << "Error: Can not open file: " << file << std::endl;
exit(1);
}
std::cout << "Ready to record IQ to file: " << file << std::endl;
return file;
}
void Source::close_file()
{
if (saveIqFile != NULL)
{
fclose(saveIqFile);
}
else
{
std::cerr << "Error: Can not close file pointer." << std::endl;
}
}

View file

@ -29,6 +29,9 @@ protected:
/// @brief True if IQ data to be saved.
bool *saveIq;
/// @brief File pointer to IQ data to be saved.
FILE *saveIqFile;
public:
Source();
@ -67,12 +70,13 @@ public:
/// @brief Open a new file to record IQ.
/// @details First creates a new file from current timestamp.
/// @return Void.
virtual void open_file() = 0;
/// Files are of format <path>.<type>.iq.
/// @return String of full path to file.
std::string open_file();
/// @brief Close IQ file gracefully.
/// @return Void.
virtual void close_file() = 0;
void close_file();
};

View file

@ -71,14 +71,9 @@ RspDuo::RspDuo(std::string _type, uint32_t _fc, uint32_t _fs,
rf_notch_fg = false;
dab_notch_fg = false;
chunk_time_nr = DEF_CHUNK_TIME_NR;
}
std::string RspDuo::set_file(std::string path)
{
char buffer[15];
gettimeofday(&start_tm, NULL);
strftime(buffer, 16, "%Y%m%d-%H%M%S", localtime(&start_tm.tv_sec));
return path + buffer + ".rspduo";
out_file_fp = saveIqFile;
capture_fg = &saveIq;
}
void RspDuo::start()
@ -148,44 +143,6 @@ void RspDuo::replay(IqData *_buffer1, IqData *_buffer2, std::string _file, bool
}
void RspDuo::open_file()
{
file = set_file(path);
char time_tx[BUFFER_SIZE_NR];
// get start date and time
gettimeofday(&start_tm, NULL);
strftime(time_tx, sizeof(time_tx), "%d %b %Y %H:%M:%S", localtime(&start_tm.tv_sec));
fprintf(stderr, "Info - start - start_tm: %s.%03ld\n", time_tx, start_tm.tv_usec / 1000);
//validate();
// open files
if (chunk_time_nr == 0)
{
out_file_fp = fopen(file.c_str(), "wb");
if (out_file_fp == NULL)
{
fprintf(stderr, "Error - start - opening output file %s\n", file.c_str());
exit(1);
}
fprintf(stderr, "Info - start - output file %s\n", file.c_str());
}
return;
}
void RspDuo::close_file()
{
if (out_file_fp != NULL)
{
fclose(out_file_fp);
}
}
void RspDuo::validate()
{
// validate decimation
@ -569,23 +526,24 @@ void RspDuo::stream_b_callback(short *xi, short *xq, sdrplay_api_StreamCbParamsT
write_fg = true;
}
if (capture_fg && write_fg && run_fg && chunk_tm.tv_sec <= current_tm.tv_sec)
{
if (out_file_fp != NULL)
{
fclose(out_file_fp);
}
out_file_fp = fopen(file.c_str(), "ab");
if (out_file_fp == NULL)
{
std::cerr << "Error - stream_b_callback - opening output file " + file << std::endl;
free(buffer_16_ar);
run_fg = false;
exit(1);
}
// init file open
// if (capture_fg && write_fg && run_fg && chunk_tm.tv_sec <= current_tm.tv_sec)
// {
// if (out_file_fp != NULL)
// {
// fclose(out_file_fp);
// }
// out_file_fp = fopen(file.c_str(), "ab");
// if (out_file_fp == NULL)
// {
// std::cerr << "Error - stream_b_callback - opening output file " + file << std::endl;
// free(buffer_16_ar);
// run_fg = false;
// exit(1);
// }
chunk_tm = current_tm;
}
// chunk_tm = current_tm;
// }
// write data to file
if (capture_fg && write_fg)

View file

@ -212,13 +212,6 @@ public:
/// @return Void.
void replay(IqData *buffer1, IqData *buffer2, std::string file, bool loop);
/// @brief Open a new file to record IQ.
/// @return Void.
void open_file();
/// @brief Close IQ file gracefully.
/// @return Void.
void close_file();
};
#endif

View file

@ -19,12 +19,6 @@ Usrp::Usrp(std::string _type, uint32_t _fc, uint32_t _fs,
gain = _gain;
}
std::string Usrp::set_file(std::string path)
{
// todo: deprecate
return "/blah2/tmp.iq";
}
void Usrp::start()
{
}
@ -98,7 +92,11 @@ void Usrp::process(IqData *buffer1, IqData *buffer2)
buffer1->unlock();
buffer2->unlock();
sleep(0.01);
// save IQ data to file
if (saveIq)
{
}
}
}
@ -106,13 +104,3 @@ void Usrp::replay(IqData *buffer1, IqData *buffer2, std::string _file, bool _loo
{
return;
}
void Usrp::open_file()
{
return;
}
void Usrp::close_file()
{
return;
}

View file

@ -3,7 +3,7 @@
/// @brief A class to capture data on the Ettus Research USRP.
/// @details Uses the UHD C API to extract samples into the processing chain.
///
/// Should work on all USRP models EXCEPT discontinued (USRP1, USRP2).
/// Should work on all USRP models.
/// Networked models require an IP address in the config file.
/// Requires a USB 3.0 cable for higher data rates.
///
@ -41,6 +41,7 @@ private:
std::vector<double> gain;
public:
/// @brief Constructor.
/// @param fc Center frequency (Hz).
/// @param path Path to save IQ data.
@ -49,10 +50,6 @@ public:
bool *saveIq, std::string address, std::string subdev,
std::vector<std::string> antenna, std::vector<double> gain);
/// @brief Get file name from path.
/// @return String of file name based on current time.
std::string set_file(std::string path);
/// @brief Implement capture function on USRP.
/// @param buffer1 Pointer to reference buffer.
/// @param buffer2 Pointer to surveillance buffer.
@ -75,13 +72,6 @@ public:
/// @return Void.
void replay(IqData *buffer1, IqData *buffer2, std::string file, bool loop);
/// @brief Open a new file to record IQ.
/// @return Void.
void open_file();
/// @brief Close IQ file gracefully.
/// @return Void.
void close_file();
};
#endif