GNSS-SDR  0.0.17
An Open Source GNSS Software Defined Receiver
fifo_reader.h
Go to the documentation of this file.
1 /*!
2  * \file fifo_reader.h
3  *
4  * \brief Header file to retrieve samples from an existing Unix FIFO
5  * \author Malte Lenhart, 2021. malte.lenhart(at)mailbox.org
6  *
7  * -----------------------------------------------------------------------------
8  *
9  * GNSS-SDR is a Global Navigation Satellite System software-defined receiver.
10  * This file is part of GNSS-SDR.
11  *
12  * Copyright (C) 2010-2020 (see AUTHORS file for a list of contributors)
13  * SPDX-License-Identifier: GPL-3.0-or-later
14  *
15  * -----------------------------------------------------------------------------
16  */
17 
18 #ifndef GNSS_SDR_FIFO_READER_H_
19 #define GNSS_SDR_FIFO_READER_H_
20 
21 #include "gnss_block_interface.h"
22 #include <gnuradio/sync_block.h>
23 #include <fstream> // std::ifstream
24 #include <string>
25 
26 /** \addtogroup Signal_Source
27  * \{ */
28 /** \addtogroup Signal_Source_gnuradio_blocks
29  * \{ */
30 class FifoReader : virtual public gr::sync_block
31 {
32 public:
33  //! \brief static function to create a class instance
34  using sptr = gnss_shared_ptr<FifoReader>;
35  static sptr make(const std::string &file_name, const std::string &sample_type);
36 
37  ~FifoReader() = default;
38 
39  //! initialize istream resource for FIFO
40  bool start();
41 
42  // gnu radio work cycle function
43  int work(int noutput_items,
44  gr_vector_const_void_star &input_items,
45  gr_vector_void_star &output_items);
46 
47 private:
48  //! \brief Constructor
49  //! private constructor called by function make
50  //! (gr handles this with public and private header pair)
51  FifoReader(const std::string &file_name, const std::string &sample_type);
52 
53  size_t read_gr_complex(int noutput_items, gr_vector_void_star &output_items);
54 
55  //! function to read data out of FIFO which is stored as interleaved I/Q stream.
56  //! template argument determines sample_type
57  // Note: template definition necessary in header file
58  // See also: https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file
59  template <typename Type>
60  size_t read_interleaved(int noutput_items, gr_vector_void_star &output_items)
61  {
62  size_t items_retrieved = 0;
63  for (int n = 0; n < noutput_items; n++)
64  {
65  // TODO: try if performance increases if we copy larger chunks to vector.
66  // how to read from stream: https://en.cppreference.com/w/cpp/io/basic_ifstream
67  std::array<char, 2 * sizeof(Type)> buffer;
68  fifo_.read(reinterpret_cast<char *>(buffer.data()), buffer.size());
69  if (fifo_.good())
70  {
71  auto real = reinterpret_cast<Type const *>(&buffer[0]);
72  auto imag = reinterpret_cast<Type const *>(&buffer[sizeof(Type)]);
73  static_cast<gr_complex *>(output_items[0])[n] = gr_complex(*real, *imag);
74  items_retrieved++;
75  }
76  else if (fifo_.eof())
77  {
78  fifo_.clear();
79  break;
80  }
81  else
82  {
83  fifo_error_output();
84  break;
85  }
86  }
87  return items_retrieved;
88  }
89 
90  //! this function moves logging output from this header into the source file
91  //! thereby eliminating the need to include glog/logging.h in this header
92  void fifo_error_output() const;
93 
94  const std::string file_name_;
95  const std::string sample_type_;
96  std::ifstream fifo_;
97 };
98 
99 /** \} */
100 /** \} */
101 #endif /* GNSS_SDR_FIFO_READER_H_ */
bool start()
initialize istream resource for FIFO
This interface represents a GNSS block.
gnss_shared_ptr< FifoReader > sptr
static function to create a class instance
Definition: fifo_reader.h:34