From 703560e683e83424c05d355a2f8eb125c5f773e5 Mon Sep 17 00:00:00 2001 From: 30hours Date: Fri, 19 Jan 2024 05:56:49 +0000 Subject: [PATCH] Progress on adding file pointers --- src/capture/Capture.cpp | 37 +++++----------- src/capture/Capture.h | 9 ++-- src/capture/Source.cpp | 47 +++++++++++++++++++- src/capture/Source.h | 10 +++-- src/capture/rspduo/RspDuo.cpp | 80 +++++++++-------------------------- src/capture/rspduo/RspDuo.h | 7 --- src/capture/usrp/Usrp.cpp | 22 +++------- src/capture/usrp/Usrp.h | 14 +----- 8 files changed, 93 insertions(+), 133 deletions(-) diff --git a/src/capture/Capture.cpp b/src/capture/Capture.cpp index c56018e..cf04fa5 100644 --- a/src/capture/Capture.cpp +++ b/src/capture/Capture.cpp @@ -25,16 +25,6 @@ void Capture::process(IqData *buffer1, IqData *buffer2, c4::yml::NodeRef config) std::unique_ptr 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 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; -} \ No newline at end of file diff --git a/src/capture/Capture.h b/src/capture/Capture.h index 097c72c..760d5bf 100644 --- a/src/capture/Capture.h +++ b/src/capture/Capture.h @@ -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 \ No newline at end of file diff --git a/src/capture/Source.cpp b/src/capture/Source.cpp index 52bf1ad..c981572 100644 --- a/src/capture/Source.cpp +++ b/src/capture/Source.cpp @@ -1,10 +1,14 @@ #include "Source.h" #include +#include +#include +#include +#include +#include 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(¤tTime); + 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; + } +} \ No newline at end of file diff --git a/src/capture/Source.h b/src/capture/Source.h index 18650a7..c99b41e 100644 --- a/src/capture/Source.h +++ b/src/capture/Source.h @@ -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 ..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(); }; diff --git a/src/capture/rspduo/RspDuo.cpp b/src/capture/rspduo/RspDuo.cpp index 70fd2b2..88ca030 100644 --- a/src/capture/rspduo/RspDuo.cpp +++ b/src/capture/rspduo/RspDuo.cpp @@ -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) diff --git a/src/capture/rspduo/RspDuo.h b/src/capture/rspduo/RspDuo.h index ae8bc6d..62bdbd3 100644 --- a/src/capture/rspduo/RspDuo.h +++ b/src/capture/rspduo/RspDuo.h @@ -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 \ No newline at end of file diff --git a/src/capture/usrp/Usrp.cpp b/src/capture/usrp/Usrp.cpp index 5577e01..177238f 100644 --- a/src/capture/usrp/Usrp.cpp +++ b/src/capture/usrp/Usrp.cpp @@ -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; -} diff --git a/src/capture/usrp/Usrp.h b/src/capture/usrp/Usrp.h index 37e0d1f..f84655f 100644 --- a/src/capture/usrp/Usrp.h +++ b/src/capture/usrp/Usrp.h @@ -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 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 antenna, std::vector 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 \ No newline at end of file