5G-MAG Reference Tools - MBMS Modem
RestHandler.h
Go to the documentation of this file.
1 // 5G-MAG Reference Tools
2 // MBMS Modem Process
3 //
4 // Copyright (C) 2021 Klaus Kühnhammer (Österreichische Rundfunksender GmbH & Co KG)
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Affero General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Affero General Public License for more details.
15 //
16 // You should have received a copy of the GNU Affero General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 //
19 
20 #pragma once
21 #include <string>
22 #include <vector>
23 #include <map>
24 #include <memory>
25 #include <libconfig.h++>
26 
27 #include "SdrReader.h"
28 #include "Phy.h"
29 
30 #include "cpprest/json.h"
31 #include "cpprest/http_listener.h"
32 #include "cpprest/uri.h"
33 #include "cpprest/asyncrt_utils.h"
34 #include "cpprest/filestream.h"
35 #include "cpprest/containerstream.h"
36 #include "cpprest/producerconsumerstream.h"
37 
38 const int CINR_RAVG_CNT = 20;
39 typedef enum { searching, syncing, processing } state_t;
40 
41 class CasFrameProcessor; // Forward declaration of CasFrameProcessor to avoid circular references.
43 
47 class RestHandler {
48  public:
52  typedef std::function<void(const std::string& antenna, unsigned fcen, double gain, unsigned sample_rate, unsigned bandwidth)> set_params_t;
53 
63  RestHandler(const libconfig::Config& cfg, const std::string& url, state_t& state,
64  SdrReader& sdr, Phy& phy, set_params_t set_params);
68  virtual ~RestHandler();
69 
73  void start() { _listener->open().wait(); }
74 
78  class ChannelInfo {
79  public:
80  void SetData( std::vector<uint8_t> data) {
81  std::lock_guard<std::mutex> lock(_data_mutex);
82  _data = data;
83  };
84  std::vector<uint8_t> GetData() {
85  std::lock_guard<std::mutex> lock(_data_mutex);
86  return _data;
87  };
88  bool present = false;
89  int mcs = 0;
90  double ber;
91  float evm_rms = 0.0f;
92  unsigned total = 0;
93  unsigned errors = 0;
94  private:
95  std::vector<uint8_t> _data = {};
96  std::mutex _data_mutex;
97  };
98 
102  std::vector<uint8_t> _ce_values = {};
103 
107  std::vector<uint8_t> _cir_values = {};
108 
112  std::vector<uint8_t> _cir_values_mbsfn = {};
113 
114 
118  std::vector<uint8_t> _corr_values = {};
119 
120 
124  std::vector<uint8_t> _corr_values_mbsfn = {};
125 
130 
135 
139  std::map<uint32_t, ChannelInfo> _mch;
140 
144  float cinr_db() { return _cinr_db.size() ? _cinr_db.back() : 0.0f; };
145 
149  float cinr_db_avg() { return _cinr_db.size() ? (std::accumulate(_cinr_db.begin(), _cinr_db.end(), 0.0f) / (_cinr_db.size() * 1.0f)) : 0.0f; };
150 
151  void add_cinr_value( float cinr);
152 
156  void set_cas_processor (CasFrameProcessor* cas_processor) { _cas_processor = cas_processor; };
157 
161  void set_mbsfn_processor (MbsfnFrameProcessor* mbsfn_processor) { _mbsfn_processors.push_back(mbsfn_processor); };
162 
163 
164  private:
165  // We need access to the processors to get the values to be displayed in the rt-wui.
167  std::vector<MbsfnFrameProcessor*> _mbsfn_processors;
168 
169  std::vector<float> _cinr_db;
170  void get(web::http::http_request message);
171  void put(web::http::http_request message);
172 
173  const libconfig::Config& _cfg;
174 
175  std::unique_ptr<web::http::experimental::listener::http_listener> _listener;
176 
180 
182 
183  bool _require_bearer_token = false;
184  std::string _api_key;
185 };
186 
state_t
Definition: RestHandler.h:39
@ syncing
Definition: RestHandler.h:39
@ searching
Definition: RestHandler.h:39
@ processing
Definition: RestHandler.h:39
const int CINR_RAVG_CNT
Definition: RestHandler.h:38
Frame processor for CAS subframes.
Frame processor for MBSFN subframes.
The PHY component.
Definition: Phy.h:42
RX Info pertaining to an SCH (MCCH/MCH or PDSCH)
Definition: RestHandler.h:78
std::vector< uint8_t > _data
Definition: RestHandler.h:95
std::vector< uint8_t > GetData()
Definition: RestHandler.h:84
void SetData(std::vector< uint8_t > data)
Definition: RestHandler.h:80
The RESTful API handler.
Definition: RestHandler.h:47
CasFrameProcessor * _cas_processor
Definition: RestHandler.h:161
void set_cas_processor(CasFrameProcessor *cas_processor)
Save the pointer to the CasFrameProcessor.
Definition: RestHandler.h:156
SdrReader & _sdr
Definition: RestHandler.h:178
std::vector< uint8_t > _cir_values
Time domain channel impulse response of the CAS.
Definition: RestHandler.h:107
std::unique_ptr< web::http::experimental::listener::http_listener > _listener
Definition: RestHandler.h:175
float cinr_db()
Current instantaneous CINR value.
Definition: RestHandler.h:144
float cinr_db_avg()
Current average CINR value.
Definition: RestHandler.h:149
std::vector< uint8_t > _ce_values
Time domain subcarrier CE values.
Definition: RestHandler.h:102
void put(web::http::http_request message)
std::vector< uint8_t > _corr_values
Correlaton samples from the sync functions.
Definition: RestHandler.h:118
void start()
Start function for the listener.
Definition: RestHandler.h:73
ChannelInfo _mcch
RX info for MCCH.
Definition: RestHandler.h:134
std::string _api_key
Definition: RestHandler.h:184
std::vector< float > _cinr_db
Definition: RestHandler.h:169
std::vector< MbsfnFrameProcessor * > _mbsfn_processors
Definition: RestHandler.h:167
void add_cinr_value(float cinr)
std::map< uint32_t, ChannelInfo > _mch
RX info for MCHs.
Definition: RestHandler.h:139
std::function< void(const std::string &antenna, unsigned fcen, double gain, unsigned sample_rate, unsigned bandwidth)> set_params_t
Definition of the callback for setting new reception parameters.
Definition: RestHandler.h:52
void set_mbsfn_processor(MbsfnFrameProcessor *mbsfn_processor)
Save the pointer to the vector cointaining the MbsfnFrameProcessors.
Definition: RestHandler.h:161
const libconfig::Config & _cfg
Definition: RestHandler.h:173
RestHandler(const libconfig::Config &cfg, const std::string &url, state_t &state, SdrReader &sdr, Phy &phy, set_params_t set_params)
Default constructor.
Definition: RestHandler.cpp:36
void get(web::http::http_request message)
Definition: RestHandler.cpp:78
ChannelInfo _pdsch
RX info for PDSCH.
Definition: RestHandler.h:129
virtual ~RestHandler()
Default destructor.
state_t & _state
Definition: RestHandler.h:177
std::vector< uint8_t > _corr_values_mbsfn
Correlaton samples from the sync functions.
Definition: RestHandler.h:124
bool _require_bearer_token
Definition: RestHandler.h:183
set_params_t _set_params
Definition: RestHandler.h:181
std::vector< uint8_t > _cir_values_mbsfn
Time domain channel impulse response of the mbsfn subframes.
Definition: RestHandler.h:112
Interface to the SDR stick.
Definition: SdrReader.h:37
static Config cfg
Global configuration object.
Definition: main.cpp:172
static std::string antenna
Antenna input to be used.
Definition: main.cpp:179
static unsigned sample_rate
Sample rate of the SDR.
Definition: main.cpp:174
static uint32_t bandwidth
Low pass filter bandwidth for the SDR.
Definition: main.cpp:177
void set_params(const std::string &ant, unsigned fc, double g, unsigned sr, unsigned bw)
Set new SDR parameters and initialize resynchronisation.
Definition: main.cpp:205
static double gain
Overall system gain for the SDR.
Definition: main.cpp:178