GNSS-SDR  0.0.19
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 <array>
24 #include <fstream> // std::ifstream
25 #include <string>
26 
27 /** \addtogroup Signal_Source
28  * \{ */
29 /** \addtogroup Signal_Source_gnuradio_blocks
30  * \{ */
31 class FifoReader : virtual public gr::sync_block
32 {
33 public:
34  //! \brief static function to create a class instance
35  using sptr = gnss_shared_ptr<FifoReader>;
36  static sptr make(const std::string &file_name, const std::string &sample_type);
37 
38  ~FifoReader() = default;
39 
40  //! initialize istream resource for FIFO
41  bool start();
42 
43  // gnu radio work cycle function
44  int work(int noutput_items,
45  gr_vector_const_void_star &input_items,
46  gr_vector_void_star &output_items);
47 
48 private:
49  //! \brief Constructor
50  //! private constructor called by function make
51  //! (gr handles this with public and private header pair)
52  FifoReader(const std::string &file_name, const std::string &sample_type);
53 
54  size_t read_gr_complex(int noutput_items, gr_vector_void_star &output_items);
55 
56  //! function to read data out of FIFO which is stored as interleaved I/Q stream.
57  //! template argument determines sample_type
58  // Note: template definition necessary in header file
59  // See also: https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file
60  template <typename Type>
61  size_t read_interleaved(int noutput_items, gr_vector_void_star &output_items)
62  {
63  size_t items_retrieved = 0;
64  for (int n = 0; n < noutput_items; n++)
65  {
66  // TODO: try if performance increases if we copy larger chunks to vector.
67  // how to read from stream: https://en.cppreference.com/w/cpp/io/basic_ifstream
68  std::array<char, 2 * sizeof(Type)> buffer;
69  fifo_.read(reinterpret_cast<char *>(buffer.data()), buffer.size());
70  if (fifo_.good())
71  {
72  auto real = reinterpret_cast<Type const *>(&buffer[0]);
73  auto imag = reinterpret_cast<Type const *>(&buffer[sizeof(Type)]);
74  static_cast<gr_complex *>(output_items[0])[n] = gr_complex(*real, *imag);
75  items_retrieved++;
76  }
77  else if (fifo_.eof())
78  {
79  fifo_.clear();
80  break;
81  }
82  else
83  {
84  fifo_error_output();
85  break;
86  }
87  }
88  return items_retrieved;
89  }
90 
91  //! this function moves logging output from this header into the source file
92  //! thereby eliminating the need to include glog/logging.h in this header
93  void fifo_error_output() const;
94 
95  const std::string file_name_;
96  const std::string sample_type_;
97  std::ifstream fifo_;
98 };
99 
100 /** \} */
101 /** \} */
102 #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:35